Hello guys,
I hope you're all doing well.
Today, I’m gonna go over one of the most complicated but awesome subjects in all of computing and that's “Virtual Storage” or “Virtual Memory”. Virtual memory is a crazy topic and we can't really get into its real guts in one article, this article is just a basic intro.
Image source: -Pixabay
All of us have a ton of digital devices like phones, computers, tablets and each one of these things is running a lot of programs all the time, these programs are called “Processes”.
The big question that we should ask ourselves is How do these hundreds of programs that are running on your phone not bump into each other at random points in time?
Remember, your computer is a system and it has limited resources, so maybe it has 1gb of RAM, 1 processor and 2 terabytes of Hard Disk space. How do all these programs share these resources and not just bump into each other while they're running?
If you're working on a new mobile game that you want to release, you don't have to worry about what other programs are running on the phone while your game is running. Do you think the creator of angry bird had to care about how his code worked alongside Facebook application code? I'm sure you already know the answer to this question but it's a big fat NO.
When programmers are developing applications, they really don't care about what else is running on the system, they just write their own applications and everything is good to go as long as they don’t have bugs. There's pretty much like infinity combinations and variations of what could be happening on any system, computer or phone at any given time and it would be ridiculous almost for a programmer to have to worry about that.
Another question we should ask ourselves is Who is exactly preventing these programs from bumping into each other?
Well, I'm sure you guys have a basic intuition of who's doing that and it's the “Operating System”.
Image source: -Pixabay
The operating system does many things but one of the most important thing it does is manage all the application memory of all the processes that are running. The operating system is doing a lot of work behind the scenes to make our lives a lot easier and even if we just accept it, and we don't really know how it works, we need to have a really deep appreciation for it.
The operating system provides this big blueprint of memory for every single process and what's really cool is that this blueprint is exactly the same for every single process. Let's just take an example that it's a blueprint for a house, so for every single process out there, the operating system it's gonna give them a blueprint of a house to use its memory.
The operating system can tell angry bird to store its data in the garage at address 5,000, when Facebook comes along, the operating system tells it the same thing. This blueprint that the operating system gives for every single process, you have to store your data at address 5,000 in the garage, it's the same for every single process and no one is treated any differently.
Image source: -Wikimedia Commons
Every single program sees the same blueprint of memory that the operating system provides but wait a minute, that would mess things up if the OS told everything to put their data at address 5,000 wouldn't they all collide with each other?
If a light bulb just went off in your head and maybe it did, this is exactly what virtual memory is. The address 5,000 is a virtual address that the operating system provides to every single one of these different processes and that virtual address actually maps to different physical addresses in the real system. Even though angry bird and Facebook both use address 5,000 to store their data, that 5,000 actually points to a different part of memory, real physical memory and the operating system manages that.
Virtual address 5,000 for angry bird could point to a real address of 20,000, while a virtual address of 5,000 for Facebook could point to address 30,000. It's virtual. It's the operating systems job to make sure that these virtual addresses never really collide in the physical space and it does a really good job with that. This is the beauty behind virtual addresses, the operating system gives the same exact blueprint of the house to every single process in the system.
Another really cool thing to think about is imagine if many processes shared the same pieces of code, say Facebook and angry bird share the same piece of software, do you think the operating system would duplicate that same piece of code between Facebook and angry bird or would it duplicate the code between 100 different processes if it was shared?
Well, the operating system is also really smart and it can recognize which pieces of code is shared and it can map those virtual addresses to actually the same place in real memory or when it knows that things can collide, it'll map the virtual addresses to different places.
In Conclusion
That's just a really high level Intuition behind virtual memory and hopefully, we all have a deep appreciation of what it's doing for us. Remember, the operating system is doing a lot of stuff for us besides memory management. Memory management is just one out of many important things that's happening behind the scenes.
Another cool little side thing is that virtual memory actually takes the coordination of hardware and software together. It's not purely a software thing. Hardware and software work together to make virtual memory work. I hope you guys enjoyed this article and if you did, don’t hesitate to smash that upvote button below.
References: 1 - 2 - 3
GIFs made by @rocking-dave and @foundation
Upvoted, once again, you have presented a complex topic in an way that is easily understood. Thank you for the read.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
On a related note, while programmers do not need to know where in memory they are storing their data, they need to be aware of how much they are storing. Depending on the programming language, the programmer may be responsible for "garbage collection," or giving memory back to the computer when it's no longer used by the program. If this is not done properly, this may result in a "memory leak," which means the program takes up more and more memory as time passes. This causes problems as it prevents other programs from working correctly due to limited resources.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
That's a good write up, thanks very much for the info. The concept of virtual memory is a very trickish one that true, a single post cannot possibly address it all.
Thanks and hope to read more from you.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit