Memory (RAM) usage monitoring on Linux
Introduction
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:/# vmstat
procs -----------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 -s
759872 K total memory
575220 K used memory
356148 K active memory
86168 K inactive memory
184652 K free memory
44048 K buffer memory
149248 K swap cache
0 K total swap
0 K used swap
0 K free swap
806545 non-nice user cpu ticks
1 nice user cpu ticks
533833 system cpu ticks
424692262 idle cpu ticks
54982 IO-wait cpu ticks
244344 IRQ cpu ticks
0 softirq cpu ticks
0 stolen cpu ticks
7190421 pages paged in
27240788 pages paged out
0 pages swapped in
0 pages swapped out
335817481 interrupts
285597986 CPU context switches
1425579890 boot time
332134 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.05
Tasks: 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 st
KiB Mem: 759872 total, 576156 used, 183716 free, 44628 buffers
KiB Swap: 0 total, 0 used, 0 free. 149652 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 36284 5312 1192 S 0.0 0.7 0:42.98 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:05.13 ksoftirqd/0
5 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 cached
Mem: 742 562 179 27 43 146
-/+ buffers/cache: 372 369
Swap: 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.