Where are my program's bits?
Chronicles of C++ Memory Management - Part 2
I’m continuing the "Intro to Memory Management in C++" series with another 'commonly known but good to remember' article. Where do your bits really live?
(click on image to expand; more details available below the image)
Turns out your bits can really come from many places - storage mediums, networks, sensors, user inputs, etc. But in order to do anything reasonably fast with them, your OS tends to move most of them to fastest medium possible - RAM memory. Do be mindful of the fact that they still have to be moved from RAM to CPU registers, as that is the only way for CPU to actually process them!
One neat little trick that OS' pull is the use of Virtual Memory. By using Virtual Addressing instead of targeting actual locations, it allows them to extend the number of reachable addresses way beyond what can actually fit into RAM. And they do this by 'offloading' stuff to other storage mediums (e.g. hard drives), which is usually referred to as 'swap memory'. This also ensures that bigger programs that can't fit everything into RAM are not going to crash due to lack of space. It does make stuff slower, sure, but at least it keeps the lights on.
Finally, an interesting piece of info that I learned recently is that "MOV" (i.e. MOVE stuff FROM x to y) is the MOST COMMONLY used CPU operation! Basically turns out that most of what your CPU is doing is moving bits FROM and TO registers! Also an 'interesting to know but not that useful' piece of info is that MOV can only move FROM and TO registers, hence you can't really instruct CPU to MOVE stuff from SSD to RAM (e.g. MOV ram_address, ssd_address). Good news is that there's a thing called "Direct Memory Access (DMA)" that makes a bridge between RAM and SSD, so that CPU can just instruct RAM to copy range of addresses and deal with other matters while it happens.
And that'd be about it for today. Even though this info is already widely known, I deemed it to be a good idea to remind ourselves WHERE stuff is coming from before takin a deeper dive into how Virtual Memory is 'split' (i.e. talking about Stacks and heaps). Speaking of Stacks & heaps, that's a topic for very next article, so, stay tuned! :)
Thanks for reading Bitesized Engineering! Subscribe for free to receive new posts and support my work.