One interesting aspect about modern operating systems is the fact that they can run programs that require more memory than the system actually has. Like the Tardis in Dr. Who, Linux memory is much bigger on the inside than on the outside.
At the extreme end, this means that if your CPU is 32-bit (meaning that it has registers that are 32-bits), you can access up to 232 bytes (that 4,294,967,296 or 4 billion). That means you would need 4 Gb of main memory (RAM) in order to to completely take advantage of this. Although many systems are currently available (2007) with 512 MB or even 1 GB, more RAM than that is rare; and 4 GB is extremely rare for a home PC.
The interesting thing is that when you sum the memory requirements of the programs you are running, you often reach far beyond the physical memory you have. Currently one of my system appears to need about 570 Mb. Although my machine only has 384 Mb. Surprisingly enough, I don’t notice any performance problems. So, how is this possible?
Well, Linux, Unix and many other operating systems take advantage of the fact that most programs don’t use all of the memory that they “require”, as you typically do not use every part of the program at once. For example, you might be using a word processor, but not currently using the spell checking feature, or printing function, so there is no need to keep these in memory at the same time. Also, while you are using your word processor, your email program is probably sitting around doing nothing.
From the user’s perspective the email program (or parts of the word processor) are loaded into memory. However, the system only loads what it needs. In some cases, they might all be in memory at once. However, if you load enough programs, you eventually reach a point where you have more programs than you have memory.
To solve this problem, Linux uses something called “virtual memory”. It’s virtual because it can use more than you actually have. In fact, with virtual memory you can use the whole 232 bytes. Basically, what this means is that you can run more programs at once without the need for buying more memory.
When a program starts, Linux does not load all of it, just the portion it takes to get started. An important aspect of virtual memory is keeping parts of the program that are not needed on the hard disk. As the process runs, when it finds it needs other parts of the program, it goes and gets them. Those parts that are never needed are never loaded and the system does not use all of the memory it appears to “require”.
If you have more data than physical memory, the system might store it temporarily on the hard disk should it not be needed at the moment. The process of moving data to and from the hard disk like this is called swapping, as the data is “swapped” in and out. Typically, when you install the system, you define a specific partition as the swap partition, or swap “space”. However, Linux can also swap to a physical file, although with older Linux versions this is much slower than a special partition. An old rule of thumb is that you have at least as much swap space as you do physical RAM, this ensures that all of the data can be swapped out, if necessary. You will also find that some texts say that you should have at least twice as much swap as physical RAM. We go into details on swap in the section in installing and upgrading.
In order to do all this, the system needs to manage your memory. This function is logically called “memory management” and is one of the core aspects of any modern operating system. Although the details are different from one operating system to the next, the basic principles apply, even between different CPU types.
In other sections of the tutorial, we will talk about the details of memory management
from both the perspective of the CPU and
the operating system kernel.