Checking Free Memory (RAM)

Video Tutorial



Overview

There are a few commands you can use to check free memory on your Linux server.    The ones commonly used are:

  • free
  • top
 
The free Command
Let's focus on using the 'free' command first.

bdowne01@web6 [~]# free -m

             total       used       free     shared    buffers     cached

Mem:          3954       3635        319          0        302       2425

-/+ buffers/cache:        907       3047

Swap:         4094        360       3734

 Note:  The free command can be run by a regular user account.   No need to be "root".


The highlighted line shows how the utilization of memory on your linux server, in megabytes.   You may replace the "-m" argument in the free command with "-g" to show the values in gigabytes, or no argument at all to show values in kilobytes.

Here is a table explaining the different columns.

Item Meaning
total The amount of memory installed on your server.
used How much memory has been consumed.
free How much memory is unallocated
shared The amount of memory used as common for multiple programs
buffers Disk cache:  permissions, inodes, file ownership
cached Disk cache:  File contents & data


Where's my memory?  Buffers & Cache Explained
The "used" and "free" values in the first row can be misleading.   In the example above, it appears that out of our 4 gigabytes of RAM, we only have 319 megabytes free.   While that's true, it's not the whole story.   

The default behaviour of Linux is to use as much memory as possible for disk buffers & cache.   If it's not currently be used by a program, Linux will cache file data there.   The first row in the output considers the buffers and cache into the values, making the "free" look very low.

However, file buffers & cache are reallocatable to programs needing memory at any time.   So the buffers and cache aren't really "used' memory, only "used" because it wasn't being used for anything else!

For that reason, the second row shows the used and free values with the buffers & cache not in the formula:

bdowne01@web6 [~]# free -m

             total       used       free     shared    buffers     cached

Mem:          3954       3635        319          0        302       2425

-/+ buffers/cache:        907       3047

Swap:         4094        360       3734

When looking at this line, things seem to make more sense.   It says we've used 907 megabytes, and have closer to 3 gigabytes free.    This is the line you should use to determine how much free memory you have.

Swap

bdowne01@web6 [~]# free -m

             total       used       free     shared    buffers     cached

Mem:          3954       3635        319          0        302       2425

-/+ buffers/cache:        907       3047

Swap:         4094        360       3734

The final row in the 'free' output is swap.   Swap is virtual memory, i.e. memory that is stored on disk.   This is a disk partition, LVM volume, or file that has been reserved to act as an "emergency fund" if all the actual RAM on the server is exhausted.   While it's normal to have a bit of swap used on your server, the "used" amount being close to the "total" amount probably means you need a RAM upgrade.


The top Command
The other command to check memory is 'top'.   While its primary use is to check process activity on the server, it does have a summarized version of the 'free' data at the upper portion of the display.   

top - 20:15:01 up 41 days, 20:24,  1 user,  load average: 0.48, 0.43, 0.30

Tasks: 164 total,   1 running, 156 sleeping,   0 stopped,   7 zombie

Cpu(s): 12.9%us,  2.3%sy,  0.0%ni, 83.1%id,  1.7%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   4049884k total,  3866108k used,   183776k free,   312408k buffers

Swap:  4193276k total,   368200k used,  3825076k free,  2609360k cached


The command runs continuously, updating the data every few seconds.   While it does show the basic values, remember to consider the buffers & cache as part of the equation.   Unlike 'free', 'top' does not offer a corrected value taking those into account.

Was this answer helpful?

 Print this Article

Also Read

File System Extension with LVM

Video TutorialOverviewWhen running the 'df' command on a TLFDS dedicated server, you will see...