This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This video is a continuation of our virtual memory series, based on the article/book I wrote on the same topic. In the previous video, we talked about what virtual memory is and why we need it. This week, we look at the size of the virtual memory space. Since virtual memory is accessed through virtual addresses, a natural question is: how much memory can a program actually address? This video answers that question, along with a few related ones: * Deriving the size of the virtual address space from first principles * Why the virtual address space is split between user space and kernel space * What canonical virtual addresses are In the next video, we will talk about the address space layout of a process. In the meantime, you can read the full article if you haven’t already. Update on the Ebook Version I also want to give a quick update on the virtual memory ebook. I’ve updated the PDF for a smoother on-screen reading experience with better color schemes and fonts. Apart from that, I’ve also created an EPUB version for those who prefer that format. You can get both from the link below. Happy reading!
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_01Hi everyone, this is Abhinav. So we have been doing a series of virtual memory related videos based on the article that I wrote on the same topic a few weeks back. So many of you have really enjoyed this format of the article and I think the topic is also something, you know, many people want to understand well. So I also want to make these videos covering the same set of topics but in a more conversational or direct manner. So in the last video,
we talked about what is virtual memory and why does it exist. And in this video, we are going to talk about the size of virtual address space of process. So before starting this video, let's quickly recap what we talked about in the last video.
SPEAKER_01So we learned that virtual memory exists as a mechanism for the operating system to safely share physical memory amongst multiple processes that are running concurrently on the same system. Because if one process is using a certain address, then that address should not be read or written by another process. And that can only be possible if the operating system is sitting between the process and the
SPEAKER_01memory itself. And that is done by the virtual memory layer. So operating system gives every process its own virtual Abhinav Upadhyay Abhinav Abhinav Upadhyay
Operating System does this mapping of virtual addresses to physical addresses and it maintains those mappings somewhere in the kernel and the memory management hardware MMU will look up those mappings and figure out what is the mapping you know where does this memory access go in physical memory and does this goes there and reads or writes on the data so that's what we learned in the last video now Now in the topic of this video is size of virtual memory So
every process receives its own private virtual memory and as a result it has this virtual address space So
SPEAKER_01what is the size of that address space? So this space consists of addresses and basically the question is how many addresses it can have or how much memory can those addresses access? so let's do that again by discussing this from the first principles so with any address how much memory you can access the question goes to the
size of the address
SPEAKER_01so for example if we have if our virtual address is just one bit then there are two possibilities you can access either byte 0 or byte 1 which means you can access up to 2 bytes of data or the size of the virtual address space is 2 bytes if your virtual address is 1 bit long if your virtual address is 2 bits then
SPEAKER_00there are 4 possible values 0 0 0 1 1 0 and 1 1 which means 4 bytes. So your virtual address space becomes 4 bytes wide. Now, in a general case,
SPEAKER_01if your virtual address is n bits, so you can get the idea, your physical address or the virtual address space will be 2 to the power n bytes. so that is the general formula so if your virtual address is n bits wide then your virtual address space will be 2 to the power n bytes so now let's talk about real hardware so mod Hardware has come in all forms and sizes but I'll stick to 64
bits in all of these videos and even in my article I'll stick to 64 bits so modern day servers laptops,
SPEAKER_00desktops are mostly 64 bits so which means so what does that mean?
SPEAKER_01so one of the or one of the implications of having a 64-bit processor is that the register size is also 64-bit wide so you have registers which are 64-bit so and what this means is that your virtual address also has to fit within a register because if
If the compiler is generating an instruction to read a particular byte in memory, so it will have to generate instruction where the address goes into the register
SPEAKER_01and then the read instruction has to be emitted. Basically, the processor will go and read the address from the register itself. So, if the address doesn't fit in a register, then it's a problem. So, which is why the maximum size of a virtual address on 64-bit hardware can be 64 bits. So, that is the maximum. But, so, which means that if your virtual address is 64 bits, then your virtual address space becomes 2 to the power 64 bytes.
which means that you can theoretically map up to 2 to the power 64 bytes of physical memory as well if your virtual address is this much then you can map the same amount of physical memory as well but this is an extremely large amount of RAM and we don't really have computers right now or it may not be possible for a very long time to have computers with this much amount of physical memory so the
SPEAKER_01hardware designers were conservative because the more there are implications on the performance of the software as well so if you create extremely large virtual address space the translation costs also increase so the hardware designers came up with a more conservative size so they said for until we have computers which can actually have RAM 2 to the
power 64 bytes of physical RAM they said that 48 bits of a virtual address can be 48 bits wide 48 bits which means that you can access the size of virtual addresses space becomes
SPEAKER_012 to the power 48 bytes and 2 to the power 48 bytes is 256 terabytes so in present day hardware usually you have virtual addresses that are 48 bits wide and that results in a virtual address space of 256 terabytes theoretically you can have 256 terabytes of physical memory with this with a 48 bits
wide virtual address
SPEAKER_01there are also some you know some Linux kernels recently where you have 57 bits wide virtual address so that has an implication on how the mapping is done but yes so the more common variation is virtual addresses that are 48 bits wide but we also have Linux kernel which has 57 bits wide virtual address so the difference is that with 48 bit wide
Virtual Address you need if you are familiar with page tables that we will cover in
SPEAKER_01the future videos so you need four levels of page tables with 57 bits virtual address you need 5 levels of base table so this basically allows as you can guess 57 bits wide virtual address means you can access or you can address much more amount of physical memory with it whereas with 48 bits wide virtual address you can access up to 256 terabytes so in this
whole video series and even in my article I have stuck to 4 level of page tables and 48 bit wide virtual address but you have to remember that you also have 5 level page tables and 47 bits wide virtual address so that is the size of the virtual address space so every process that is created receives this much amount of virtual
SPEAKER_01address space so it is possible to map that much amount of visual memory Now you must be wondering, even today we don't have hardware which has 256 terabytes of physical memory. So how does it really work? So for example, your laptop may have 16 GB of RAM and your process has 256 terabytes of virtual address space. So how does that really work? Is it possible that you can access any address in this range and it will work?
and not really so this is possible like this difference between virtual addresses space and physical address space is possible because this mapping of virtual to physical memory is done by the kernel and the kernel is aware of how much physical
SPEAKER_01memory that is available so it will make sure that any process cannot Abhinav but having a consistent virtual address space makes things simpler because the same operating system kernel compiled can run on a variety of
devices so on a laptop with 16 GB of RAM and on a server with 256 GB of RAM the same kernel can be
SPEAKER_01compiled and run so they don't have to keep you know Different kind of code or handling in the code to handle different sizes of physical memory. Because the kernel basically abstracts away the process of mapping. So that is the size of virtual memory addresses space and how the operating system makes sure that mappings are within the limits of the physical memory.
now there is one more point here even though you have 256 terabytes of address space virtual address space not all of that is available to the process itself so
SPEAKER_00this is split into half so there is so it is split into half so there is kernel address space and then there is user space addresses so on 64 bit systems it is split evenly so you
have 128 terabytes
SPEAKER_01addressable in the user space and 128 terabytes
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...