Useful Linux commands for examining memory usage and what the numbers mean Let’s look at some basic commands that report on memory usage. The first that probably comes to mind is free. The free command will tell you about used and unused memory and about swap space. Physical memory is the random access storage provided by the RAM modules plugged into your motherboard. Swap is some portion of space on your hard drive that is used as if it is an extension of your physical memory.The first line of the free command’s output contains the column headings. The second, labeled Mem: displays information on how physical memory is being used. The -m option displays the information in terms of megabytes rather than kilobytes (the default).$ free -m total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 Just looking at these numbers, we see that this system has roughly 2 GB of RAM and that nearly 95% of it is used. If we look at the Swap: line in the output, we see that the swap space appears to be unused. This system has 4 GB of swap, twice the size of the physical memory — following a common rule of thumb for setting up swap space. $ free -m total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 -/+ buffers/cache: 322 1703 Swap: 4094 0 4094 Between the Mem: and Swap: lines, we see a line labeled -/+ buffers/cache. This is probably the trickiest part of understanding free’s output. This line shows how much of the physical memory is used by the buffer cache. In other words, this shows how much memory is being used (think “borrowed”) for disk caching. And don’t forget that you should like disk caching because it makes the system run much faster.So, while at first glance, this system appears to be running short of memory, it’s actually just making good use of memory that’s currently not needed for anything else. The key number to look at in the output above is, therefore, the 1703. This is the amount of memory that would be made available to your applications if they need it.Adding a -t to the free command gives you a line of totals at the bottom. Look carefully and you’ll notice that the -/+ buffers/cache figures are not considered in the totals line. $ free -tm total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 -/+ buffers/cache: 322 1704 Swap: 4094 0 4094 Total: 6121 1922 4198 If your system is busy and you want to watch how memory is changing, you can run free with a -s (seconds) argument that causes the command to give you totals every X seconds. For example::-) free -ms 10 total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 -/+ buffers/cache: 322 1704 Swap: 4094 0 4094 total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 -/+ buffers/cache: 321 1704 Swap: 4094 0 4094 total used free shared buffers cached Mem: 2026 1922 103 0 491 1109 -/+ buffers/cache: 321 1704 Swap: 4094 0 4094 total used free shared buffers cached Mem: 2026 1922 104 0 491 1109 -/+ buffers/cache: 321 1704 Swap: 4094 0 4094 You would terminate the looping with a ^C. Another option is to use the watch command. This will give you a two-second updated display::-) watch free Every 2.0s: free Sun Jun 10 10:54:55 2012 total used free shared buffers cached Mem: 2074952 1966704 108248 0 486960 1156036 -/+ buffers/cache: 323708 1751244 Swap: 4192956 128 4192828 You can change the interval used by the watch command by providing a -n # option where # is replaced by a number of seconds (e.g., -n 10). 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. Software DevelopmentTechnology Industry