Virtual Memory and Memory Paging

As explained in another section, every program being executed and every piece of data being manipulated must be in main memory (RAM). The amount and speed of main memory is possibly the most important factor in determining the power of a computer. However, in the early days of computers, memory chips were small capacity, scarce, and expensive. Because of this a memory management scheme called "paging" was invented.

To increase the amount of available memory, the operating system allocates part of the hard disk as "virtual" or "pretend" memory. When RAM is close to full and another program or data requests memory, a chunk of data called a "page" is written to a special file on the hard disk called the "page" file or "swap" file. The data written to the page file is the data that was least recently accessed.

Then the requested program or data is moved into the RAM area that was written to the page file. Now the programs and⁄or data that was swapped into RAM can be executed and⁄or manipulated.

However, what if a request is made for something in the chunk of data that was written out to the page file? Then another least recently accessed page of RAM is written to the page file, and the previously paged out data is swapped back into RAM.

This action of paging data back and forth between RAM and the hard disc continues transparently while the system is operating. A page of data that is moved from RAM to virtual memory in order to make room for programs or data being moved into RAM. When a page of data in virtual memory is needed back in RAM, another page swap occurs.

Virtual memory page table

A page table is used by a virtual memory system map between physical frames and virtual pages. A valid bit (v) tells if the page is currently in main memory or if it must be retrieved from virtual memory. If the page is in main memory v is set to 1. When a page is taken from disc and put back into main memory, v is set to 0. A modifed bit (m) tells if a page has been written to while in main memory. If it hasn't been modified, m is set to 1. If it hasn't been modified, and a copy of it is in virtual memory, it doesn't need to be written to disc, which speeds the system up. If it has modified, m is set to 0 and the page must be written to virtual memory.

Now, you might think this memory paging involves a lot of overhead, and since today, RAM chips are now much larger, abundant, and relatively cheap, that virtual memory schemes would be obsolete. Unfortunately, software has become much more bloated and the demand to run multiple programs simultaneously (multitask) has increased. So the need for virtual memory is more now than ever.

In order to minimize the overhead of virtual memory, proper virtual memory configuration is required. The main perimeters of virtual memory management that can be configured are the page size and the swap file size. With most operating systems you can configure the page size between 4KB and 1GB. There are tradeoffs with page size. Smaller pages can be swapped out more quickly, but more frequent page swaps may be required.

You can also configure the size of the swap file (page file) on the hard disk. It is generally recommended to set the page file equal to the size of main memory RAM. However this depends upon the operating system and what you're using the computer for. If you're using Linux, and you're using the "hibernate" function, you may want to set a swap partition (Linux uses a swap partition rather than page file) 1.5 or 2 times the size of main memory RAM.

However the Windows operating system uses the file hiberfil.sys to save the system data and status when hibernating. The page file isn’t involved. With either operating system, if your application uses huge data files, you might want to increase the size of the swap file.

More Computer Architecture Articles:
• Digital Logic Transfer Characteristics
• Arduino Microcontroller Development Platform
• Using The I2C Bus
• Operating System Services
• Operating System Memory Paging
• Load Balancing Multiple CPUs in Symmetric Multiprocessing
• Computer Buses
• Intoduction to Digital Electronics
• Computer Video Display
• Introduction to Computer System Main Memory Operation