Home » How-to, Linux, Newbie

How to check memory usage on linux

18 November 2008 2 Comments

Under linux we have multiple posibilities to see the memory status. Some of this commands can show you a lot of informations about swap, buffers, caches, pages, virtual and real memory, and some are showing just simple informations about used memory. Let’s start with the most common one: free.

$ free
total       used       free     shared    buffers     cached
Mem:       2063332    2029448      33884          0      65728     722372
-/+ buffers/cache:    1241348     821984
Swap:      1951856     700852    1251004

If we run this command without any parameter it will show us all values in kilobytes, but free command can also show us all these values in bytes (-b), megabytes(-m) and gigabytes (-g). Also, if you want only the memory and swap values then you can omit buffers/cache line with -o.

$ free -m -o
total       used       free     shared    buffers     cached
Mem:          2014       1983         31          0         64        705
Swap:         1906        684       1221

In the above example you can see a simple output of free command without buffers/cache and with values expressed in megabytes. If you want to survey this values then just run free command with -s switch what will activate the continuous polling of the command.

$free -m -o -s 5
             total       used       free     shared    buffers     cached
Mem:          2014       1991         23          0         64        696
Swap:         1906        684       1221

             total       used       free     shared    buffers     cached
Mem:          2014       1982         32          0         64        696
Swap:         1906        684       1221

The second linux application for survey memory usage is vmstat. vmstat reports information about processes,block IO, memory, paging, traps, cpu and not only memory. Is a fantastic command when you want to survey your system to find the hardware and software glitches.

$ vmstat 3 3
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
3  0 700796  29152  65736 716784    2    2    38    55   51   34 11  1 86  1
0  0 700796  29144  65736 716784    0    0     0     0  179  348  7  0 92  0
0  0 700796  29144  65740 716784    0    0     0    29  176  419  2  0 98  0

The above example will poll vmstat 3 times at an interval of 3 seconds. Using -S switch we can choose the values output: k or K or m or M switches outputs between 1000, 1024, 1000000, or 1048576 bytes.

$ vmstat -SM
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
3  0    684     31     64    696    0    0    38    55   51   35 11  1 86  1

Field description for vmstat command (from vmstat linux manual)

r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.

swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)

si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).

bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).

in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.

These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

So if si/so are not close to 0 then your system is swapping, if is swapping your CPU (and other resources like disk) it will be very loaded. In that case adding memory will improve your system.

The third linux application is a very used one: top. Top can show you interactively the memory and CPU:

top - 11:58:36 up 8 days,  2:20,  7 users,  load average: 0.18, 0.18, 0.17
Tasks: 173 total,   2 running, 170 sleeping,   0 stopped,   1 zombie
Cpu(s):  3.7%us,  0.3%sy,  0.0%ni, 95.7%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Mem:   2063332k total,  2030412k used,    32920k free,    65824k buffers
Swap:  1951856k total,   700732k used,  1251124k free,   712420k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                         
 6430 root      20   0  599m 282m 9.8m S  3.0 14.0 181:22.15 Xorg                                                                                            
12652 sacx      20   0 18992 1320  932 R  0.7  0.1   0:00.30 top                                                                                             
18537 sacx      20   0  171m  57m 6472 S  0.7  2.9  98:39.55 skype32                                                                                         
20184 sacx      20   0  571m  31m  15m S  0.7  1.6  47:58.02 amarokapp                                                                                       
 7310 sacx      20   0  343m  23m 9740 R  0.3  1.2   2:39.71 gnome-terminal                                                                                  
17359 sacx      20   0  299m  11m 8532 S  0.3  0.6   8:27.32 truecrypt                                 

With top you can also view what application is loading your memory and CPU.

I hope this short “how to” will help you to a better understanding of linux power.


  • Geek Question Answer said:

    Thanks for the Post!!

    If you could post how to check memory used by process that will help lot.

  • admin (author) said:

    Hi GeekQuestionAnswer,

    Good idea.

    Use ps -e -orss,args –sort rss to sort all memory processes by memory size.


Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.