Making sense of memory usage on Linux

Useful Linux commands for examining memory usage and what the numbers mean

RELATED TOPICS
Page 2 of 2

Another command that will provide you with some information on how your memory is being used is top. While top is one of my all-time favorite commands for looking at performance, even its memory statistics need a little interpretation. Let's begin with the first number on the Mem: and Swap: lines. These are the totals for RAM and swap space and should be consistent with the numbers that we get by looking at the output from the free command. The same holds true of the used and free figures on these lines. Where the top output gets a little confusing is in the buffers and cached figures. Note that we saw these number in the output of the free command, but they were associated with the Mem: statistics, With top, they're on the and Swap: lines. But, no, these figures actually refer to two RAM statistics and describe aspects of physical memory.

:-) top
top - 18:54:11 up 38 days, 19:58,  7 users,  load average: 0.02, 0.01, 0.00
Tasks: 181 total,   1 running, 179 sleeping,   1 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni, 99.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2074952k total,  1967968k used,   106984k free,   503416k buffers
Swap:  4192956k total,      128k used,  4192828k free,  1135640k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
18751 shs       15   0  2424  980  724 R  2.0  0.0   0:00.01 top
    1 root      15   0  2160  592  516 S  0.0  0.0   0:01.07 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.07 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.77 migration/1
    6 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1
    7 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1
    8 root      RT  -5     0    0    0 S  0.0  0.0   0:00.14 migration/2
    9 root      39  19     0    0    0 S  0.0  0.0   0:00.02 ksoftirqd/2
   10 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/2
   11 root      RT  -5     0    0    0 S  0.0  0.0   0:00.32 migration/3
   12 root      39  19     0    0    0 S  0.0  0.0   0:00.04 ksoftirqd/3
   13 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/3
   14 root      10  -5     0    0    0 S  0.0  0.0   0:00.02 events/0
   15 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 events/1
   16 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 events/2

The buffers number represents in-memory blocks that result from the kernel accessing the disk, such as when the kernel needs to read the contents of files. The cached figure tells us how much RAM is being used to cache the content of recently read files. The buffer figure increases when the file system layer is bypassed while the cache grows when the file system is used. Both grow as read operations increase.

One last command that you might use when looking at memory use is vmstat. In vmstat output, you should see some of the same numbers, but in different places. The swpd figure shows used swap space while free shows free memory. The buff and cache figures are the same figures as the buffer and cached figured from free and top. What you don't see are the

numbers for overall memory size or the reassuring -/+ buffers/cache that give you an idea how much flexibility your memory system can muster if there is demand for more memory.

:-) vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0    128 106612 503380 1135668    0    0     2     2    3    2  0  0 100  0  0

The best command for getting a quick and accurate view of memory use on a Linux system is probably the free command, provided you factor in an understanding of what the -/+ buffers/cache line is telling you and don't worry too much if the other numbers tell you that you're using 95% of the memory you have available.

RELATED TOPICS
| 1 2 Page 2
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies