Search This Blog

Wednesday, December 30, 2009

Virtual Memory Behavior in Linux

Introductory Terms:

Virtual Memory: A system that combines physical memory along with some sec-
ondary storage device to give the appearance that a computer system has more
physical memory than is actually installed. A virtual memory system tries to ef-
ficiently allocate physical memory (RAM) among many competing demands, in-
cluding: kernel code, kernel global data, dynamically allocated kernel memory,
kernel caches (buffer, page, swap, and slab), application code, application stack
space, static application memory, and application heap.

Page: Kernel memory management works on top of the computer system hardware
which manipulates memory in units called pages. Page size is determined solely by
the underlying hardware. The page size is 4096 bytes on IA32 hardware platforms.

Buffer Cache: The buffer cache holds filesystem metadata, such as the inode tables,
direct blocks, indirect blocks, journals, superblocks and filesystem bitmaps. Buffer
replacement is triggered by new buffer allocations which cause the VM to evict old
buffers from the system’s inactive list.

Page Cache: This is a read/write cache for files stored on a filesystem. It is re-
ported as Cached when the file /proc/meminfo is consulted. It includes regular
files opened for reading and writing, along with mmaped files, and pages of exe-
cutables currently in memory. (In 2.4.18 and later kernels, the page cache also con-
tains filesystem directories.) In addition, objects which appear in the file system
space but have no associated backing store (such as /proc files, pipes and FIFOs)
use memory in the page cache.

Swap Cache: This is a read/write cache for process data and stack pages that have
been written to the swap device. It is reported as SwapCached when the file
/proc/meminfo is consulted. The swap cache should be considered a virtual
cache, since there is no separate bookkeeping for it in the kernel. It is, however, a
convenient concept, and we shall refer to it in subsequent sections.

Active List: This is a collection of pages which the kernel has determined to be in
active use. The size of this list is reported as Active when the file /proc/meminfo
is consulted.

Inactive List: This set of pages resides in memory, but the kernel has determined
that they are not in active use. These pages are candidates for eviction should the
system come under memory pressure.

Out of Memory Killer (OOM): The OOM is an algorithm which is invoked when the
system senses a potentially fatal shortage of memory. The kernel attempts to kill
processes on a ’most good for least harm’ basis. This algorithm is only invoked
when the system is truly out of memory and swap, and more memory is needed
by the kernel to avoid a deadlock situation.

VM Killer: This algorithm is invoked after a critical shortage of memory has been
detected. It indiscriminately kills the process whose request for memory was im-
mediate and critical to the process, and where the request was impossible to satisfy.
In 2.4.9 and later kernels, this algorithm should never trigger. Such a trigger is con-
sidered a kernel bug. (In experimental kernels, the most commonly seen causes
of the VM killer invocation are poorly written filesystems and pathological VM
implementations.)

No comments: