RAM and Virtual Memory: Overview

Tech Articles
Diagnosing issues

Memory Shortages
UNIX memory shortages impact performance once virtual memory is impacted. Therefore, when we are investigating possible memory shortages, we are investigating virtual memory as well.

RAM and Virtual Memory
Virtual memory allows computers to run processes that, in the aggregate, need more memory than the computer has. Computers always need more memory than what is available. Virtual memory is the most successful solution to this issue.

Before getting into specifics, it is worth mentioning two things...

1) RAM (Random Access Memory) is significantly faster and more expensive than storage. Virtual Memory allows the OS to augment RAM with storage but at a cost of slowness. This is a tradeoff where too much reliance on virtual memory has a performance impact. This impact is often:
  • additional I/O (writing data to storage)
  • additional CPU cycles (managing virtual memory)

2) Many advances in computer technology result from components being separated into abstraction layers. This allows a solution to replace one layer. Virtual memory is another example of this type of abstraction: memory is presented to the processors as a range of addresses. The processors use those addresses and, when necessary, the OS handles bringing the data into RAM. (Another common example that explained elsewhere is the OSI model which breaks networking into different layers to allow improvement in one area without recreating the operating system's networking from scratch).

Here is an overview of how virtual memory is used:
  • Virtual memory is a layer of memory addresses that map to hardware addresses.
  • The OS maps the virtual to physical addresses in a pages table.
  • When a processor needs information it thinks is in memory, it tries to read it from the virtual memory address
  • The virtual memory address is converted to a physical one
  • The data is read by the processor

The MMU
To implement virtual memory, it is necessary for the computer to have special memory management hardware known as an MMU (Memory Management Unit).

An MMU makes virtual memory possible by:
  • Organizing memory into pages
  • Creating an additional set of memory addresses to access these pages

The MMU puts a memory addresses through a translation step prior to each memory access. This allows a certain memory address, for example 123, to be directed to physical address 82043 at one time, and physical address 20468 another time.

The MMU makes the tracking of the memory addresses possible by dividing RAM into contiguous sections of memory of a specific size. These sections of memory are called pages and are handled by the MMU as single entities. The overhead of individually tracking the virtual to physical translations for billions of bytes of memory would be too great.

Without an MMU, when the CPU accesses RAM, the actual RAM locations never change, memory address 123 will always the same physical location within RAM.


Paging and swapping
Paging and swapping are often used interchangeably to mean taking data out of memory and writing it to storage. The difference is subtle.

Swapping is the movement of an entire process onto disk.

Paging is the movement of units of data (the pages) in memory to disk. Typically, paging involves less memory than what is needed by a process.

Processes are swapped out. Data is paged out.

Page Faults
A big part of measuring memory capacity is the counting of page faults.

A page fault is not a failure of the system. Instead, a page fault is when the OS needs to look for a page of memory. If a process needs a memory page that is unavailable, the process triggers a page fault. The kernel responds by taking control of the CPU away from the process in order to get the page ready.

There are two kinds of page faults: minor and major.

Minor page faults are, indeed, minor. The page is in fact in memory, but the MMU (memory management unit) needs to ask the kernel for the location. If the MMU does not have enough space to store all the page locations for a process or the process requests more memory, a minor page fault may occur.

To put major page faults in perspective, you must remember the old saying among systems administrators that every read from storage is a tragedy. When a desired memory page isn't in RAM, the kernel must load it from storage. This is a major page fault. A major page fault is not necessarily bad. After all, when you start an application and the kernel first loads data and code from the disk, that is a major page fault.

Major page faults indicate an issue when the computer starts to run out of memory. If the computer is overloaded it may run into a situation that is called thrashing. As a process becomes runnable, it attempts to access a page that has been swapped. This forces the process to be swapped but also an unused page from another process is swapped out. Processes are being swapped more than they are doing actual work. Major page faults are the most important indication that this is happening.

Seeing Page Faults
The utility /usr/bin/time can show you page faults and other data on a processes resource usage.

Try:
/usr/bin/time cal

In OSX, try:
/usr/bin/time -l cal

You can also see page faults in ps. (Assume that the PID is 1313)
ps -o pid,min_flt,maj_flt 1313

In OSX, those options are:
ps -o pid,minflt,majflt 1313

Tools for Memory Usage Analyses
The most common tools used to analyze memory usage are:
  • vmstat: virtual and physical memory statistics
  • ps: process status
  • prstat: per-process memory usage
  • top
  • sar: historical statistics


Suggestions for Future Learning
This information will be in the second edition of UNIX For Application Support Staff . The ETA for the second edition is December 1, 2016.