Making sense of memory usage on Linux

By  

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.

Photo Credit: 

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question