设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

如何在Linux中找出内存消耗大的进程

发布时间:2019-11-06 21:13 所属栏目:117 来源:Magesh Maruthamuthu
导读:【线下技术沙龙】11月23日,多云时代开启企业业务新高度,安全如何与时俱进? 很多次,你可能遇见过系统消耗了过多的内存。如果是这种情况,那么好的办法是识别出 Linux 机器上消耗过多内存的进程。我相信,你可能已经运行了下文中的命令以进行检查。如果没
【线下技术沙龙】11月23日,多云时代开启企业业务新高度,安全如何与时俱进?

 如何在Linux中找出内存消耗大的进程

很多次,你可能遇见过系统消耗了过多的内存。如果是这种情况,那么好的办法是识别出 Linux 机器上消耗过多内存的进程。我相信,你可能已经运行了下文中的命令以进行检查。如果没有,那你尝试过哪些其他的命令?我希望你可以在评论中更新这篇文章,它可能会帮助其他用户。

使用 top 命令 和 ps 命令 可以轻松的识别这种情况。我过去经常同时使用这两个命令,两个命令得到的结果是相同的。所以我建议你从中选择一个喜欢的使用就可以。

1) 如何使用 ps 命令在 Linux 中查找内存消耗最大的进程

ps 命令用于报告当前进程的快照。ps 命令的意思是“进程状态”。这是一个标准的 Linux 应用程序,用于查找有关在 Linux 系统上运行进程的信息。

它用于列出当前正在运行的进程及其进程 ID(PID)、进程所有者名称、进程优先级(PR)以及正在运行的命令的绝对路径等。

下面的 ps 命令格式为你提供有关内存消耗最大进程的更多信息。

  1. # ps aux --sort -rss | head
  2.  
  3. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  4. mysql 1064 3.2 5.4 886076 209988 ? Ssl Oct25 62:40 /usr/sbin/mysqld
  5. varnish 23396 0.0 2.9 286492 115616 ? SLl Oct25 0:42 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -s malloc,256M
  6. named 1105 0.0 2.7 311712 108204 ? Ssl Oct25 0:16 /usr/sbin/named -u named -c /etc/named.conf
  7. nobody 23377 0.2 2.3 153096 89432 ? S Oct25 4:35 nginx: worker process
  8. nobody 23376 0.1 2.1 147096 83316 ? S Oct25 2:18 nginx: worker process
  9. root 23375 0.0 1.7 131028 66764 ? Ss Oct25 0:01 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  10. nobody 23378 0.0 1.6 130988 64592 ? S Oct25 0:00 nginx: cache manager process
  11. root 1135 0.0 0.9 86708 37572 ? S 05:37 0:20 cwpsrv: worker process
  12. root 1133 0.0 0.9 86708 37544 ? S 05:37 0:05 cwpsrv: worker process

使用以下 ps 命令格式可在输出中仅展示有关内存消耗过程的特定信息。

  1. # ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head
  2.  
  3. PID PPID %MEM %CPU CMD
  4. 1064 1 5.4 3.2 /usr/sbin/mysqld
  5. 23396 23386 2.9 0.0 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -s malloc,256M
  6. 1105 1 2.7 0.0 /usr/sbin/named -u named -c /etc/named.conf
  7. 23377 23375 2.3 0.2 nginx: worker process
  8. 23376 23375 2.1 0.1 nginx: worker process
  9. 3625 977 1.9 0.0 /usr/local/bin/php-cgi /home/daygeekc/public_html/index.php
  10. 23375 1 1.7 0.0 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  11. 23378 23375 1.6 0.0 nginx: cache manager process
  12. 1135 3034 0.9 0.0 cwpsrv: worker process

如果你只想查看命令名称而不是命令的绝对路径,请使用下面的 ps 命令格式。

  1. # ps -eo pid,ppid,%mem,%cpu,comm --sort=-%mem | head
  2.  
  3. PID PPID %MEM %CPU COMMAND
  4. 1064 1 5.4 3.2 mysqld
  5. 23396 23386 2.9 0.0 cache-main
  6. 1105 1 2.7 0.0 named
  7. 23377 23375 2.3 0.2 nginx
  8. 23376 23375 2.1 0.1 nginx
  9. 23375 1 1.7 0.0 nginx
  10. 23378 23375 1.6 0.0 nginx
  11. 1135 3034 0.9 0.0 cwpsrv
  12. 1133 3034 0.9 0.0 cwpsrv

2) 如何使用 top 命令在 Linux 中查找内存消耗最大的进程

Linux 的 top 命令是用来监视 Linux 系统性能的最好和最知名的命令。它在交互界面上显示运行的系统进程的实时视图。但是,如果要查找内存消耗最大的进程,请 在批处理模式下使用 top 命令。

你应该正确地 了解 top 命令输出 以解决系统中的性能问题。

  1. # top -c -b -o +%MEM | head -n 20 | tail -15
  2.  
  3. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  4. 1064 mysql 20 0 886076 209740 8388 S 0.0 5.4 62:41.20 /usr/sbin/mysqld
  5. 23396 varnish 20 0 286492 115616 83572 S 0.0 3.0 0:42.24 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :82 -T 127.0.0.1:6082 -S /etc/varnish/secret -s malloc,256M
  6. 1105 named 20 0 311712 108204 2424 S 0.0 2.8 0:16.41 /usr/sbin/named -u named -c /etc/named.conf
  7. 23377 nobody 20 0 153240 89432 2432 S 0.0 2.3 4:35.74 nginx: worker process
  8. 23376 nobody 20 0 147096 83316 2416 S 0.0 2.1 2:18.09 nginx: worker process
  9. 23375 root 20 0 131028 66764 1616 S 0.0 1.7 0:01.07 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  10. 23378 nobody 20 0 130988 64592 592 S 0.0 1.7 0:00.51 nginx: cache manager process
  11. 1135 root 20 0 86708 37572 2252 S 0.0 1.0 0:20.18 cwpsrv: worker process
  12. 1133 root 20 0 86708 37544 2212 S 0.0 1.0 0:05.94 cwpsrv: worker process
  13. 3034 root 20 0 86704 36740 1452 S 0.0 0.9 0:00.09 cwpsrv: master process /usr/local/cwpsrv/bin/cwpsrv
  14. 1067 nobody 20 0 1356200 31588 2352 S 0.0 0.8 0:56.06 /usr/local/apache/bin/httpd -k start
  15. 977 nobody 20 0 1356088 31268 2372 S 0.0 0.8 0:30.44 /usr/local/apache/bin/httpd -k start
  16. 968 nobody 20 0 1356216 30544 2348 S 0.0 0.8 0:19.95 /usr/local/apache/bin/httpd -k start

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读