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).