[Understanding] : Virtual Memory

As a Performance Engineer you will come across Virtual memory very often specially when monitoring or debugging Memory issues. Virtual memory along with below mentioned terminologies are used very loosely across the industry.
– Page, Page frame, Page fault, Minor/Major fault, Paging, Swapping etc.

This article is an attempt to understand Virtual memory in detail theoretically, in the context of Computer Architecture.

What is virtual memory?

Virtual memory is not the real memory. It is an abstraction layer provided to each process. It is meant to simplify the software development, leaving the physical memory placement to operating system.
To put in very simple terms, the purpose of Virtual memory is to use the hard disk as an extension of RAM, thus increasing the available address space a process can use. Using Virtual memory, system can address more memory than it actually has, and it uses the hard drive to hold the excess. This area on the hard drive is called a page file, because it holds chunks of main memory on the hard drive.

Virtual memory is implemented by using the concept of Paging. So let’s understand paging.

Paging:

  • Paging is a fine grained approach of managing and freeing main memory.
  • The main memory(RAM) and the Virtual memory are divided into fixed-size blocks (which are known as Page frames and Pages respectively) and a process before it gets on to the main memory is also divided in to the same sized blocks. This fixes size is known as Page size which is usually 4 Kbytes.
  • We need not have the whole process (all the pages) in main memory at once for the process to run; virtual memory allows a program to run when only specific pieces are present in memory. The parts not currently being used are stored in the page file on disk.
  • Typically, these fixed sized blocks of the program are off loaded to page file (hard drive) when not needed, which is known as page-out and are brought in to memory when needed, which is known as page-in.
  • The pages can be of two type : Dirty or Clean :
    • Imagine one of the page blocks in the main memory, which the kernel thread writes to page file (hard drive). This page is not instantly freed from the main memory, but is kept around in case something wants to do further IO on it. However, if the memory crunch occurs on main memory, this page will be eligible for clean up since a copy of it is already present in page file.
    • Now, if the page is modified in the main memory, the copy of it in the page file will become outdated. These modified pages in the main memory are marked as Dirty. The page-out will require it to be written to page file and update it.
    • However, if the page in the main memory is not modified after the write to page file, it will be marked as Clean. So, when the main memory crunch occurs, the kernel can just off load them to free the memory without requiring to make any disk write.
    • A kernel thread will wake up periodically and push the cache marked as Dirty to the disk and sync the data, and also mark the cache back as Clean.

Demand Paging :

Most of the operating systems these days support demand paging in which mapping of Virtual memory to Physical memory is done on demand. This reduces the CPU overhead of mapping during the memory allocation until they are really required. In demand paging, when the page is accessed for the first time, there is initially no page mapping a Page fault occurs.
– If the mapping can be satisfied from another page in the memory, it is called Minor fault.
– If the page fault requires hard drive access to get the uncached memory file, it is called Major fault.

Swapping vs Paging :

It is important to note that Swapping and Paging are two different things, although both are used very loosely. Swapping is the movement of entire process between main memory and swap device, where as in Paging parts of not used pages are paged-out. Swapping severely hurts the performance as it involves large number of IO’s to run again. When engineers say that the system is swapping, it is very important to be sure if it is Swapping or Paging.


Conclusion:

  • As a performance engineer, it is important to understand what is virtual memory and how it is used.
  • It is normal for the memory usage to grow as the system boots up and the process starts running as system uses the free memory to cache the file system. In other words, if there is more free memory, the system will keep the pages in main memory (cached) even after page-outs to increase the performance. The bottom line is : If there is free memory, use it for something useful. If the need arises for freeing main memory, the kernel will quickly free the cached and buffer memory for the new needs.
  • So to summarize, when we do “free -m” on a linux machine, there will be a cached section. This holds the page cache data. After page faults, a copy is kept here to make next reads faster. However if memory crunch happens, the kernel can clean out cached/buffers quickly. Thus the memory accounted under “cached” and “buffers” are also as good as free memory.

More on how to monitor Page faults, Page in/outs and what to make out of it in coming articles.

Happy tuning!

3 thoughts on “[Understanding] : Virtual Memory

  1. Very good article! You go straight to the important points, while making it easy to follow and understand. Exactly what I was looking for, thank you!

    Like

Leave a reply to sscsoumitra Cancel reply