Understanding System Memory

Memory is one of the primary price-determining factors when buying server hardware or virtual server hosting. It is therefore important to monitor and understand memory usage.

To get an overview of system memory usage, use the following:

$ free -m

The m switch tells free to display the numbers in terms of megabytes. Other possible options include b for bytes, k for kilobytes (default), and g for gigabytes.

The first row lists total, used, and free physical memory. Physical memory refers to the memory that is provided by physical memory sticks on your machine, as opposed to virtual memory which is the space that your computer borrows from a long-term storage medium to extend memory.

In comparison to physical memory, virtual memory is slower but cheaper on a per-GB basis. Modern systems have enough physical memory that they rarely need virtual memory unless a specific purpose (like software compilation or video rendering) favors it.

The second row (-/+ buffers/cache) lists total, used, and free physical memory that isn’t part of the buffers or cache. Linux, and other operating systems, cache parts of the long-term storage medium (hard disk) in memory for quick access, because physical memory is much faster than a hard drive.

In reality, an operating system tries to fill memory that isn’t being used by programs with cache and buffer data from the hard disk. The more cache and buffer space it uses, the faster a system is.

The second row is the most important one when examining system memory usage.

The third row lists statistics for swap space, which is another term for virtual memory.

The other important memory-monitoring program is top. Try it out:

$ top

You can press q or Ctrl + C at any point to quit from top.

By default, it updates every 3 seconds with statistics about system processes. Here is a brief explanation of the data in the columns:

You should focus primarily on the amount of residential memory (RES) that is listed by a process. Shared memory is secondary.

If a process is taking up an abnormal amount of memory, check to see if there are performance-tweaking options in their configuration. For common server processes like apache and mysql, there are.

Additionally, top lists sizes for physical and virtual memory utilization including the buffers and the cache.