Saturday, January 21, 2023

Side Trip - Virtual Memory

It has come to my attention lately that the concepts around virtual memory and how your computer actually uses memory are not well understood by many people. What follows is a general description of what is going on when you are using your computer, and why at some points in time, your computer will seem to get really slow when there’s a lot of open processes.

1) The Physical

Your typical computer has two main types of storage:  Short term storage (Random Access Memory - RAM), and Long Term storage (the Hard Disk). Think of RAM as a great big whiteboard that is constantly being written on, and the Hard Disk as containing the contents of the whiteboard you want to keep as documents, programs, etc. 

A common computer configuration these days has 8 Gigabytes (Gb) of RAM, and 512 Gb of Hard Disk (HDD).

Then there’s your CPU.  Most processors these days are 64 bit processors which have an address space that is 64 bits wide, and typically work with data in 64 bit chunks called “words”.  A 64 bit address space means that the computer can address up to 2^64 individual memory locations - about 18 Exabytes.  Address space is the absolute addressable memory for the CPU.  18 Exabytes is 1024^6 bytes, and 1 Gb is 1024^3 bytes (a byte being 8 bits, and commonly understood to be the base unit of meaningful information in a computer) - so an Exabyte is an absolutely huge range compared to the physical address space of your computer.

2) The Basics 

Your computer’s operating system, whether it is UNIX, Linux, or Windows, play some interesting tricks.  Let’s say you have 8Gb of physical memory, and your processor thinks it has access to a lot more than that, how do we deal with that requirement?  The first point is that we don’t just let the processor address all 18 Exabytes of its address space - usually the motherboard address bus takes care of that for us - and that’s partially why most motherboards restrict to a maximum of maybe 128 Gb of physical memory. The size of board required to have a full 18 Exabytes of physical memory would be mind-boggling. (Not to mention the cost!)

That means off the top, your operating system has to play some tricks to let the CPU think it’s got more memory than it actually does. In UNIX and Linux systems, that’s usually done by setting aside a chunk of disk as a “swap” partition, Windows usually makes this a discrete file somewhere on the disk, but the effect is similar. 

The operating system then divides physical memory (RAM) up into chunks called “pages”, and it will move pieces of a process onto the swap partition in order to keep enough RAM available to facilitate the demands of active processes for memory. This process is often called “paging”.  So if you hear your technical support person saying “your computer is paging itself to death”, it basically means that the computer is spending all of its time moving stuff to and from the swap area, and you have either too many processes for the amount of RAM, or your swap is too small - sometimes both. 

The combination of physical memory plus swap is the total amount of virtual memory you have available. Of course, this will lead some to think “wow - so if I buy a terabyte of disk, I’ll have tons of available memory”.  That isn’t always true. Windows, for example is much more conservative with its swap, and too large a swap file can cause other problems.  In UNIX/Linux type environments a huge swap may not cause problems, but it won’t buy you much if you aren’t utilizing it all. Remember, swap is much slower than RAM, even with today’s solid state disks.  So using swap always comes with a performance penalty of some kind. 

3) The Kernel View

The kernel of your operating system treats memory as a great big heap.  When a process is started, it requests a certain amount of memory to run in, and the kernel does some juggling to make that happen.  If there’s lots of physical RAM available, chances are your new process gets its request fulfilled, and everything continues on its way.  

If, on the other hand, RAM is getting a bit tight, the kernel may look at the usage of other processes, and force some parts of other processes into swap. More cleverly written algorithms will examine how often the process is accessing a particular page, and only move parts deemed to be “low frequency” off to swap, gambling that the cost in time is “cheaper” than moving other parts. 

The kernel acts as a librarian here, it controls how much physical memory and swap space your process is allowed to occupy at any given time. It may even have specific limits so that no one process can consume your entire computer (and yes, one ill-behaved program really can ruin your day).  

4) The Process View 

There are some interesting aspects of virtual memory that affect your process as well. Virtual memory schemes also mean that every process will “think” that it has access to the entire computer.  In other words, each process will believe that it has access to the full memory resources of the computer.  In principle, that means you can have multiple processes that all want 8Gb of memory, and they will all think they have access to it all.

Even more intriguing here is that the processes will all believe they can access the full 64 bit address space of the CPU - which is utterly huge.  There are a lot of reasons this is beneficial, but those are mostly of interest to programmers.  

The process thinks it has all the memory in the world, but it’s up to the kernel to maintain that illusion.  That means the kernel sits in the background and moves parts of the program in and out of RAM from the swap area on your HDD.

If this sounds a bit like it’s “black magic”, it kind of is - few people have the experience to really understand the underlying code in the kernel without spending a long time in that space. 

5) Tying It All Together

In a Virtual Memory environment, the operating system kernel “owns” the physical resources of RAM and HDD, and makes your computer appear much bigger than it actually is by using a combination of RAM and HDD space (swap) to give your processes the illusion that they all have full access to the entire computer. 

You sacrifice a bit of long term storage on your HDD to make this work, and there is definitely a performance price compared to everything being in RAM, but overall you end up with a lot more flexibility in how you use your computer.  

Virtual Memory is what enables you to have a ton of PDF documents open, your word processor going, and your web browser all at the same time. Much of the time, the stuff you aren’t actively using is sitting on swap, and just enough is left in memory to allow it to become active when you switch to that process. 

6) What Virtual Memory Isn’t

Virtual Memory is not infinite - even when it seems so vast.

Virtual Memory should not be confused with “Virtual Machines” (VMs).  A virtual machine is a software abstraction of a complete computer - from hardware all the way to software. Just to make your life more interesting, a Virtual Machine may well have its own virtual memory system when it’s running, and your computer hosting it will subject that VM to the rules of your computer’s Virtual Memory system.  

7) What About Buying A Computer?

The rule of “the more RAM the merrier” still applies. If you have an option to buy 16Gb instead of 8Gb, do it. Software continues to grow in complexity and resource needs.  4 years ago, 8Gb was lots, now the floor is moving towards 16Gb.  (… and back in the 1980s, Bill Gates once quipped “640 Kb ought to be enough for anybody”) 

For disk drive, look for arrangements that will let you put the OS on one drive, and your files on another drive entirely. Today’s solid state SSD and NVME drives are awesome, but they do have a limited number of read/write cycles before they degrade.  Being able to swap out your operating system drive without losing your data is a good thing. 

… and for goodness’ sake - backups matter. Get a nice big external disk for backups and use it regularly.  

No comments:

About “Forced Treatment” and Homelessness

I need to comment on the political pressure to force people experiencing addiction into treatment. Superficially, it seems to address a prob...