如何监控Linux服务器的内存使用情况?
Linux 系统
如何监控Linux服务器的内存使用情况?
2024-02-20 16:09
监控云服务器的内存使用情况非常重要。特别是使用Redis或Memcached等数据库时,如果云服务器内存耗尽,网站或者应用就会响应变慢甚至无法访问。幸运的是,Linux提供了一些好用的工具用于监控内存使用情况,这些工具可以通过命令行使用。这篇文章集合了一些常见的内存监控工具,供系统管理员参考。
vmstat
在命令行输入vmstat,系统会显示当前的可用内存。
[email protected]:/# 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
如果需要显示更多详细信息,请在执行vmstat时加上-s参数。返回结果的第一行显示了云服务器当前的可用内存。
[email protected]:/# 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是最常用的查看CPU和内存情况的工具之一。一个典型的执行结果如下。
[email protected]:/# 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
free工具是最直观的显示内存使用情况的工具。
[email protected]:/# free -m total used free shared buffers cached Mem: 742 562 179 27 43 146 -/+ buffers/cache: 372 369 Swap: 0 0 0
以上输出结果显示了当前云服务器总共有多少内存(单位为M),使用了多少内存,以及剩余多少内存。
以上工具可用来手工监控内存的使用情况。我们也可以写一个脚本来自动执行这些工具,当内存达到一定的阈值时,以邮件或者短信方式发送提醒。
label :
- linux
- 内存使用数据