# 9.4. Memory Management¶

Modern PC processors often have memory addresses given by 48 bits. That means they can store address $$2^{48}$$ (256TB - over 200 trillion) different byte location. On the other hand, a nice personal computer may only have 8 GB (or ~8 billion bytes) of actual main memory (RAM). In other words, a program may think it can store information in 200 trillion different locations but there are only 8 billion actual places where a byte can go in memory.

Not only are there significantly more addresses that a program is allowed to use than actually exist, every program that is running thinks it gets to use nearly the entire memory space. Two programs may both decide to store a piece of information at byte address #100000 - the operating system needs to make sure that neither program destroys the other’s information and that they can both get to what they stored.

Operating systems can address both of these issues through memory address virtualization. The basic idea is that the operating system takes any memory reference from a program (a virtual address) and remaps it to a location in main memory. What a program thinks is address #100000 may actually be at address #203434. When a second program asks to store information at address #100000, that is mapped to a completely different location, say #834255.

However, all this juggling is completely invisible to running programs. The operating system provides the abstraction of a nice clean sequential set of memory addresses for each program to use while doing complicated tricks in the background to allocate the physical hardware among all the different running processes. The diagram below demonstrates how this works

If main memory is full, the operating system moves information to secondary memory (like the hard drive) but pretends it is still in main memory (this trick is known as virtual memory). Although this allows the computer to pretend there is more RAM available, it also means that any access of that memory involves retrieving it from the hard drive, which is tens of thousands of times slower. When needed, it will be swapped into memory and something else will be moved to the hard drive. If you have ever started up too many programs at once and had your computer grind to a crawl as you switch from one to another, you have seen this in practice - memory is being swapped in/out to the very slow hard drive as you switch from one program to another.

Below is an illustration of how the virtual memory trick works:

Process A thinks it has 8 addresses to use. So does process B. Orange blocks show memory for process A that is currently actually in the physical RAM, blue blocks show memory for B that is currently in RAM.

When Process A asks the operating system for “memory location 1” because it wants to access the data A1, the operating system looks up where that memory is really stored, finds the data in location 3 and then gives that back to the process. Process A will never know that what it thinks of as “memory location 1” is actually physically stored in location 3.

If Process B asks for “memory location 1”, the operating system will give it the memory that is actually stored at location 6. If it asked for “memory location 2”, it would get the data that is actually stored in location 1.

The gray blocks are blocks that are not currently in RAM. They are sitting on the hard drive, which is being used as backup storage for the RAM since there is too much total information to all fit in main memory at the same time.

Now imaging Process B asks the operating system for “memory location 8”.

That memory block is not currently in RAM, it is on the hard drive. So, the operating system needs to move it back into RAM (the hard drive is too slow to work from directly).

To do that, the operating system needs to make room by evicting some other memory block. It may chose to do this based on which block has been used the least or used the least recently. We will assume it choses the block at location 5, “A3”.

The needed block, “B8” has now been placed into location 5. Meanwhile, “A3” has finished moving to the hard drive to be stored until it is needed again.

Finally now, the operating system can complete the request by giving Process B the memory block “B8”.

Process A doesn’t know that “A3” is no longer actually in main memory. If it goes to access it, the operating will use the same procedure to restore it from the hard drive by evicting some other block of memory.

#### Self Check

You have attempted of activities on this page