What to look for in Linux memory usage

Linux kernel memory management is largely automated, but there are a few ways to analyze and boost Linux memory usage.

Linux servers allocate real and virtual memory to processes, managing memory usage via swap. Understanding memory...

types and how processes share them will help you optimize Linux memory usage.

Executing program processes allocate memory from the Linux kernel at start-up, taking a sum of virtual memory. Virtual memory on Linux is unlimited; processes will always be able to allocate memory when they start. The Linux kernel maps allocated virtual memory to real memory via swap.

More tips on Linux administration

Learn about when to use Linux memory swap

Read about how to improve server performance with Linux swap

Get more information in our Linux server guide

Unlike Windows, where memory swap can slow programs down, Linux memory swap is advantageous, thanks to the way in which Linux analyzes processes' allocated memory page use. The Linux kernel runs a Least Recently Used algorithm to determine which memory pages need to be in RAM and which do not. In some cases, letting the Linux kernel swap faster can improve memory performance.

A program's process normally requests more virtual memory than it actually needs. In Linux's top program, requested virtual memory appears in the VIRT and resident memory (RSS) columns. When the process begins to do something with its allocated memory pages, the pages move to RAM, appearing as RSS. Monitoring Linux kernel memory usage allows administrators to keep only those memory pages that are needed frequently. Less frequently accessed pages can either be discarded by the kernel or swapped.

Processes on the Linux server use anonymous memory, which is related to code, as well as data-related file memory. Anonymous memory must always be directly available, so if there is pressure on the memory resources, pages that are marked as anonymous memory should be swapped.

Data memory is a different story. When a process reads data from a disk, it stays in the cache, allowing the process to serve data quickly the next time the data are requested. Under memory pressure, the kernel can simply discard data memory. There is no performance benefit of moving unused file memory to swap, so discarding it is a better choice.

The Linux kernel distinguishes between active and inactive memory when it determines which memory to discard or swap. If inactive memory hasn't been used recently, there is no harm in moving inactive memory to swap or discarding it. The /proc/meminfo file shows the difference between active memory and inactive memory for anonymous and file memory (see example 1).

Example 1. The difference between active memory and inactive memory from the Linux kernel.

[[email protected] ~]# less /proc/meminfo

MemTotal: 7971636 kB

MemFree: 2653648 kB

Buffers: 250868 kB

Cached: 2974052 kB

SwapCached: 0 kB

Active: 2530380 kB

Inactive: 2323604 kB

Active(anon): 1633532 kB

Inactive(anon): 125712 kB

Active(file): 896848 kB

Inactive(file): 2197892 kB

Unevictable: 0 kB

Mlocked: 0 kB

SwapTotal: 10059772 kB

SwapFree: 10059772 kB

Dirty: 132 kB

Writeback: 0 kB

AnonPages: 1629052 kB

Mapped: 162396 kB

Shmem: 130192 kB

Slab: 296332 kB

SReclaimable: 233344 kB

SUnreclaim: 62988 kB

In this example, there is a lot of active anonymous memory, but only little inactive anonymous memory. If the system is under memory pressure, the kernel can move its 125 MB of inactive anonymous memory to swap. It shouldn't swap any of the 1.6 GB of memory that has been flagged as active page memory, because that will degrade performance as memory pages constantly have to be moved between RAM and disk. If the amount of swap used is larger than the amount of inactive (anonymous) memory, you should add more RAM to optimize Linux memory usage.

For ideal Linux memory usage, the amount of swap that is actually used should not be more than the amount of inactive (anonymous) memory pages. The amount of active (file) memory is the amount of cached memory the kernel requires. Anything more than that won't improve server performance.

Inactive file memory is flagged to be discarded when the kernel requires more memory. Since the Linux kernel doesn't have anything better to do with it, inactive file memory is kept in cache. If the data is discarded, it does no harm.

Sander van Vugt is an independent trainer and consultant living in the Netherlands. Van Vugt is an expert in Linux high availability, virtualization and performance and has completed several projects that implement all three. Sander is also a regular speaker on many Linux conferences all over the world. He is also the writer of various Linux-related books, such as Beginning the Linux Command LineBeginning Ubuntu Server Administration and Pro Ubuntu Server Administration.

Dig Deeper on Linux servers