如何在 Linux 中找到最消耗内存和 CPU 资源的进程
source link: https://os.51cto.com/article/710106.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何在 Linux 中找到最消耗内存和 CPU 资源的进程-51CTO.COM
Linux 是一种开源且非常流行的操作系统,是在 Internet 上部署服务器和应用程序的首选方案。它非常受欢迎,因为它的命令行界面和内置工具可以帮助您快速执行任务并节省大量时间。
有时,由于高内存和 CPU 使用率,您的应用程序或网站非常缓慢或无响应。在这种情况下,您可以使用 ps 和 top 命令来确定哪些进程正在占用系统上的所有资源。
在这篇文章中,我们将解释如何在 Linux 中找到最消耗内存和 CPU 资源的进程。
- 运行 Linux 的服务器
- 在您的服务器上配置的 root 密码
使用 ps 命令按内存和 CPU 使用率查找排名靠前的进程
ps 是一个 Linux 命令行实用程序,具有许多选项,可帮助您以不同格式显示输出。
要列出 Linux 中所有正在运行的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ps aux
这将向您展示所有正在运行的进程的概览:
您可以使用带有–sort参数的 ps 命令按内存和 CPU 使用率对输出进行排序。
使用 sort 参数的语法是:
ps aux --sort
要按 CPU 使用率查找运行次数最多的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort -%cpu
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
linuxmi 1891 3.1 6.7 3992296 269128 ? Ssl 07:27 0:21 /usr/bin/gnom
linuxmi 2186 1.8 4.1 1268748 167000 ? Sl 07:27 0:13 /snap/snap-st
root 1362 1.5 1.1 455372 44384 ? Ssl 07:23 0:14 /usr/libexec/
linuxmi 1648 1.2 2.1 357848 87008 tty2 Sl+ 07:27 0:09 /usr/lib/xorg
linuxmi 3696 1.0 0.1 19928 5720 pts/2 Ss 07:39 0:00 bash
要按内存使用情况查找运行次数最多的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort -%mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
linuxmi 1891 3.3 6.8 3992304 271720 ? Ssl 07:27 0:27 /usr/bin/gnom
linuxmi 2186 1.6 4.1 1268748 167000 ? Sl 07:27 0:13 /snap/snap-st
linuxmi 1648 1.6 2.2 357308 88936 tty2 Rl+ 07:27 0:13 /usr/lib/xorg
linuxmi 2352 0.1 2.1 764304 85808 ? Ssl 07:27 0:01 /usr/libexec/
linuxmi 2435 0.8 1.9 1199048 77292 ? Ssl 07:27 0:06 /usr/libexec/
linuxmi 1847 0.0 1.9 604112 76340 ? Ssl 07:27 0:00 /usr/libexec/
如果只想显示前 10 个内存消耗最大的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort -%mem | head -10
如果您只想显示命令名称而不是命令的完整路径,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ps -eo pid,ppid,cmd,comm,%mem,%cpu --sort=-%mem | head -10
PID PPID CMD COMMAND %MEM %CPU
6310 5891 /usr/bin/gnome-shell gnome-shell 6.8 16.1
6528 5891 /snap/snap-store/582/usr/bi snap-store 2.6 8.4
5944 5935 /usr/lib/xorg/Xorg vt2 -dis Xorg 2.1 5.9
6658 5891 /usr/libexec/xdg-desktop-po xdg-desktop-por 2.1 1.4
6272 5891 /usr/libexec/gnome-remote-d gnome-remote-de 1.9 0.5
7249 5891 /usr/libexec/gnome-terminal gnome-terminal- 1.8 3.8
6719 6310 gjs /usr/share/gnome-shell/ gjs 1.7 1.8
6467 6285 /usr/libexec/evolution-data evolution-alarm 1.7 1.2
6120 5891 /snap/snapd-desktop-integra snapd-desktop-i 1.5 1.1
使用 top 命令按内存和 CPU 使用率查找排名靠前的进程
top 是另一个内置的 Linux 命令行实用程序,可用于显示 Linux 中所有正在运行的进程。您可以使用 top 命令的各种选项来根据您的要求过滤输出。
您可以使用带有-o标志的 top 命令来显示最消耗内存的进程:
linuxmi@linuxmi:~/www.linuxmi.com$ top -o %MEM
top - 07:51:04 up 28 min, 1 user, load average: 0.35, 0.74, 0.54
任务: 300 total, 3 running, 297 sleeping, 0 stopped, 0 zombie
%Cpu(s): 4.5 us, 5.2 sy, 0.0 ni, 89.9 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
MiB Mem : 3890.2 total, 1039.1 free, 1170.5 used, 1680.6 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2468.3 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
6310 linuxmi 20 0 4064512 277496 132776 S 7.6 7.0 0:21.48 gnome-s+
6528 linuxmi 20 0 1137864 104216 67168 S 0.0 2.6 0:04.68 snap-st+
5944 linuxmi 20 0 359812 90444 56248 S 7.6 2.3 0:11.92 Xorg
6658 linuxmi 20 0 763856 85380 65372 S 0.0 2.1 0:00.85 xdg-des+
7249 linuxmi 20 0 1202060 76628 58760 R 4.6 1.9 0:05.55 gnome-t+
6272 linuxmi 20 0 604112 76256 61388 S 0.0 1.9 0:00.35 gnome-r+
6719 linuxmi 20 0 2840584 69488 50784 S 0.0 1.7 0:01.46 gjs
6467 linuxmi 20 0 920600 68768 52932 S 0.0 1.7 0:00.72 evoluti+
6120 linuxmi 20 0 224424 60256 44948 S 0.0 1.5 0:00.74 snapd-d+
7459 linuxmi 20 0 897360 58708 46240 S 0.0 1.5 0:00.58 update-+
1362 root 20 0 455504 44548 19876 S 0.0 1.1 0:23.34 package+
6046 linuxmi 20 0 570112 40568 33368 S 0.0 1.0 0:00.13 goa-dae+
6462 linuxmi 20 0 298024 39556 30536 S 0.0 1.0 0:00.97 vmtoolsd
如果您只想显示前 10 个内存消耗的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ top -o %MEM | head -n 16
top - 07:54:12 up 31 min, 1 user, load average: 0.01, 0.38, 0.43
任务: 297 total, 1 running, 296 sleeping, 0 stopped, 0 zombie
%Cpu(s): 10.0 us, 10.0 sy, 0.0 ni, 80.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3890.2 total, 1037.7 free, 1171.8 used, 1680.7 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2467.0 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
6310 linuxmi 20 0 4064524 277464 132776 S 5.6 7.0 0:23.08 gnome-s+
6528 linuxmi 20 0 1137864 104216 67168 S 0.0 2.6 0:04.71 snap-st+
5944 linuxmi 20 0 362476 90444 56248 S 0.0 2.3 0:13.51 Xorg
6658 linuxmi 20 0 763600 85380 65372 S 0.0 2.1 0:00.87 xdg-des+
7249 linuxmi 20 0 1202444 77116 58828 S 0.0 1.9 0:06.73 gnome-t+
6272 linuxmi 20 0 604112 76256 61388 S 0.0 1.9 0:00.35 gnome-r+
6719 linuxmi 20 0 2840584 69392 50784 S 0.0 1.7 0:01.53 gjs
6467 linuxmi 20 0 920600 68768 52932 S 0.0 1.7 0:00.72 evoluti+
6120 linuxmi 20 0 224424 60256 44948 S 0.0 1.5 0:00.74 snapd-d+
如果您只想显示前 10 个 CPU 消耗量最高的进程,请运行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ top -o %CPU | head -n 16
top - 07:57:03 up 34 min, 1 user, load average: 0.20, 0.31, 0.40
任务: 299 total, 1 running, 298 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5.3 us, 13.2 sy, 0.0 ni, 81.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3890.2 total, 1060.1 free, 1149.1 used, 1681.0 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2489.7 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
7732 linuxmi 20 0 21916 4596 3724 R 11.8 0.1 0:00.04 top
6310 linuxmi 20 0 4064572 277584 132776 S 5.9 7.0 0:29.58 gnome-s+
7249 linuxmi 20 0 1203916 78376 58948 S 5.9 2.0 0:10.31 gnome-t+
1 root 20 0 166496 11688 8152 S 0.0 0.3 0:06.31 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.07 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par+
5 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns
7 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker+
在这篇文章中,我们解释了如何在 Linux 中找到最消耗 CPU 和内存的进程。您可以使用这些命令来解决 Linux 中与性能相关的问题。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK