This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
In my last article, I covered virtual memory in depth. I now want to complement that with a more direct video series explaining virtual memory and its internals.
This first video builds a first-principles understanding of what virtual memory is, why it exists, and why programmers should care about it.
If you’ve already read the article, I’d be curious to know whether this explanation adds more clarity or gives you a new way to think about the topic.
The ebook/PDF version of the full article is also available for anyone who prefers reading it in a polished, downloadable format.
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. A few weeks back, I wrote this long article on virtual memory. It's almost 25,000 words and 60 pages in PDF format. It has been received really well. I think many of you like the new format of a dialogue-based explanation. And many of you have also purchased a PDF and I really appreciate the support.
so but I also understand that this sort of format is not for everyone so many
SPEAKER_01people prefer more direct to the point explanations So I wanted to do that So I decided that I will make a series of short videos covering the same set of topics But instead of doing a story-based coverage, I would do more crisper, more direct explanations But again, following the same first principles, exploration of every topic that I talk about
SPEAKER_01So in that series, this is going to be the first video. In this video, we are going to be talking about what is virtual memory, why do we need it, and why should we care about it. So instead of giving you that same textbook definition, I'll start with the first principle explanation. So we'll go to the bottom, start from the problem that exist for which virtual memory was designed.
SPEAKER_01And once we understand the problem, virtual memory will automatically emerge as the obvious solution. And once that solution, you connect those dots, everything above that layer, every other aspect of virtual memory will also start to make sense. So let's start to talk about that problem. So virtual memory is very strongly tied to our programming model because at the end of the day, it is how we access memory in our code.
SPEAKER_01So in modern days, when we write code, we are writing code in higher level languages like Java, Python, JavaScript, Rust. And in those languages, we have a lot of abstraction between our code and the hardware. So we have a compiler, we have the runtime, we have even the operating system, which is managing the memory for us. So we usually don't care about how do we access how the memory looks like. So let's go to a bare metal model where none of these
abstractions exist.
SPEAKER_01So only hardware and our code exists. So let's assume we have a very simple hardware with a very small amount of memory. Let's say 8 bytes of memory. So 1, 2, 3, 4, 5, 6.
SPEAKER_00So I'll name these addresses. These are the physical memory addresses. so this is physical memory um and so here we don't have a compiler we don't have an assembler no runtime not even an operating system uh and uh let's also assume that
SPEAKER_01uh Here, only one program can run at a time. So if we are writing a program, we can assume that only our process will be running when it executes on that system. So for such a system, if we had to write a program, let's say a simple program like this, which stores the value 10 into memory,
SPEAKER_0012 into memory, and Then, you know, does something like adds them together and writes the result back into memory.
SPEAKER_01So if you were writing, you know, code in a higher level languages, a compiler would have translated it into a machine instruction. But here we have to do that ourselves. So if you think about it, what does this mean? A is equal to 10. We are saying that we want to store the value somewhere in memory. a compiler or a runtime would have associated A with an address in memory here that job is left to us so we have to allocate memory and here
because we know that when this program executes only that process will be executing so we can freely use any
SPEAKER_01of these available eight Addresses in memory so because no one else will be using that at that time so we can if we are writing the machine code ourselves so if we are writing the machine instructions write 10 we can hard code the address even itself similarly for storing 12 we can hard code the address in the program itself so the binary itself has the physical addresses hardcoded so
when the hardware starts to execute it will simply write and into the physical address p1 this is all
SPEAKER_00saved because only one program is running but in real world in today's time we have machines where hundreds of processes run in parallel either due to multi-core or because the operating system does context switching so it executes one process then switches to another executes that and so on so all these processes which are running concurrently they are sharing the same limited amount of physical memory so you know you can think like four processes are
SPEAKER_01running at the same time and each using Some amount of physical memory. So this approach, the way I showed here, where physical addresses are hard-coded in the program code itself, that approach will not work because P1 at the time of execution might be in use by another process. So this approach is not really viable. So this is the problem. We want to be able to execute several processes concurrently
all of them sharing the same limited amount of physical memory but safely at the same time also efficiently so we don't want the processes to be contending like figuring out what addresses are safe to use so the it has to be fast at the same time safe so how do we solve that so that is a problem so in computer science there is a famous saying that every problem can be solved by one layer of interaction So if we add one layer of indirection between our code and the
physical memory,
SPEAKER_01that would solve this problem. So what that means is that if we have a layer in here, I'll just draw it here.
SPEAKER_00Let me start again, so I'll draw it more clearly. so now let's what we need is one layer of indirection so we have our physical memory p1 p2 p3 p4 and so on now we are adding one layer of indirection which is
SPEAKER_00virtual memory This also has a tracer, so we are calling it v1, v2, v3, v4, v5, v6, v7, so on. now the so basically let me first so this is called virtual memory which means it
is virtual it means that it does not really exist in the physical reality so it's memory which is you know a software abstraction which is mimicking physical memory
SPEAKER_01so it has addresses you can read and write these addresses but in physical reality there is nothing like that so it's all implemented in software with some help from the hardware
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...