Memory (RAM) usage monitoring on Linux


Monitoring RAM resources of your VPS is very important. Especially, if you use caching systems such as Redis or Memcached. If your server runs out of memory, or runs short on memory, your website or service can stop or may run slow.

Luckily, Linux gives you a few handy tools that you would need to monitor memory usage. These tools can be easily used from the command line. This short (and sweet) article will give you an idea how to use those commands, and you can pick the one that fits your needs the most.

1. vmstat command

Type in vmstat to your command line and the program will display how much free memory is available.

root@localhost:/# vmstatprocs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st 3  0      0 185332  43880 150176    0    0     2     6    8    7  0  0 100  0  0

If you need more details, run vmstat with the -s or --stats parameter. Check the first line of the output to see how much free memory is available on your server.

root@209:/# vmstat -s759872  K total memory575220  K used memory356148  K active memory86168   K inactive memory184652  K free memory44048   K buffer memory149248  K swap cache0       K total swap0       K used swap0       K free swap806545  non-nice user cpu ticks1       nice user cpu ticks533833  system cpu ticks424692262 idle cpu ticks54982   IO-wait cpu ticks244344  IRQ cpu ticks0       softirq cpu ticks0       stolen cpu ticks7190421 pages paged in27240788 pages paged out0       pages swapped in0       pages swapped out335817481 interrupts285597986 CPU context switches1425579890 boot time332134  forks

2. top command

This is a widely known command to check both memory and CPU usage.

top - 03:20:50 up 49 days,  8:55,  1 user,  load average: 0.00, 0.01, 0.05Tasks:  87 total,   1 running,  86 sleeping,   0 stopped,   0 zombie%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 stKiB Mem:    759872 total,   576156 used,   183716 free,    44628 buffersKiB Swap:        0 total,        0 used,        0 free.   149652 cached MemPID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND1 root      20   0   36284   5312   1192 S  0.0  0.7   0:42.98 init2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd3 root      20   0       0      0      0 S  0.0  0.0   0:05.13 ksoftirqd/05 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H

3. free command

This is my favorite, and probably the most simple command to view memory stats.

root@209:/# free -m              total       used       free     shared    buffers     cachedMem:           742        562        179         27         43        146-/+ buffers/cache:        372        369Swap:            0          0          0

The example above shows you how much total memory your VPS has (in Megabytes), how much memory is used, and how much memory is free.

4. htop command

This command is similar to the top command, but in my opinion, it gives you a better idea about the memory state of your server.

Conclusion

The commands listed above can be used to manually monitor memory usage. You can also write a simple script using your preferred programming language to run these commands and alert you (by email, for example) if your memory resources are low.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article

No comments

Powered by Blogger.