How to Monitor Memory Usage on Linux Cloud Servers?

21-02-2024 03:09:26

Monitoring the memory usage of cloud servers is crucial, especially when using databases such as Redis or Memcached. If the cloud server runs out of memory, websites or applications may respond slowly or even become inaccessible. Fortunately, Linux offers a variety of useful tools for monitoring memory usage, accessible via the command line. This article compiles a list of common memory monitoring tools for system administrators' reference.

vmstat

Typing vmstat into the command line displays the current available memory.

root@zhaomu.com:/# 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

For more detailed information, execute vmstat with the -s option. The first line of the returned result shows the current available memory of the cloud server.

root@zhaomu.com:/# 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

top

top is one of the most frequently used tools to check CPU and memory status. A typical output looks like this.

root@zhaomu.com:/# top
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

free

The free tool provides the most intuitive display of memory usage.

root@zhaomu.com:/# free -m
              total       used       free     shared    buffers     cached
Mem:           742        562        179         27         43        146
-/+ buffers/cache:        372        369
Swap:            0          0          0

The output above shows the total memory (in MB) of the current cloud server, how much memory is being used, and how much memory remains.

These tools can be used for manual monitoring of memory usage. Additionally, one can write a script to automatically execute these tools and send alerts via email or SMS when memory reaches a certain threshold.