查看 Linux 中进程使用的交换内存使用情况
1. 概述
在本教程中,我们将讨论如何检查使用交换内存的进程。首先,我们将介绍/proc 目录以查看它包含的内容以及我们如何从中提取进程的信息。之后,我们将编写一个 shell 脚本,该脚本将自动提取进程使用的交换使用信息。
最后,我们将使用smem 工具作为替代方法来检查使用交换内存的进程。
2. /proc目录
**在 Linux 上,proc是一个特殊目录,其中包含有关 Linux 内核、其配置和加载到物理内存中的进程的信息。**一旦我们启动到 Linux 机器,操作系统就会创建这个目录。此外,该目录中的虚拟文件在磁盘上没有物理大小。一旦我们阅读它们,这些文件中的信息就会即时生成。
现在我们对proc目录有了一个概念,让我们看看它里面有什么:
$ ls -l /proc
total 0
dr-xr-xr-x 9 root root 0 Feb 17 15:35 1
dr-xr-xr-x 9 root root 0 Feb 17 15:35 10
dr-xr-xr-x 9 root root 0 Feb 17 15:35 100
...
dr-xr-xr-x 9 hey hey 0 Feb 17 22:12 34160
...
当我们查看此目录中的项目列表时,我们会注意到有很多编号目录、命名目录和其他虚拟文件。但是,我们只关心编号的目录,因为它们对应于每个正在运行的进程。目录号其实就是进程的PID。
正如我们在上面的代码片段中看到的,它还列出了用户进程。所以,让我们列出“34160”目录中的项目,看看它包含什么:
$ ls -l /proc/34160
total 0
...
-r--r--r-- 1 hey hey 0 Feb 17 22:12 stat
-r--r--r-- 1 hey hey 0 Feb 17 22:12 statm
-r--r--r-- 1 hey hey 0 Feb 17 22:12 status
...
目录中的虚拟文件将包含有关该进程的大量信息。但在我们的例子中,我们对状态虚拟文件感兴趣,因为它包含内存使用信息。
由于我们只对交换使用感兴趣,我们将从文件中grep VmSwap字段:
$ cat /proc/34160/status | grep VmSwap
VmSwap: 0 kB
现在我们知道如何检查进程使用的交换内存,我们可以编写一个 shell 脚本来打印使用交换内存的进程列表。
2.1. 使用交换内存列出进程的 Shell 脚本
shell 脚本将遍历proc目录中的每个编号目录,并打印它从status文件中提取的交换使用情况。它还将打印进程的名称和 PID:
#!/bin/bash
overall=0
for status_file in /proc/[0-9]*/status; do
swap_mem=$(grep VmSwap "$status_file" | awk '{ print $2 }')
if [ "$swap_mem" ] && [ "$swap_mem" -gt 0 ]; then
pid=$(grep Tgid "$status_file" | awk '{ print $2 }')
name=$(grep Name "$status_file" | awk '{ print $2 }')
printf "%s\t%s\t%s KB\n" "$pid" "$name" "$swap_mem"
fi
overall=$((overall+swap_mem))
done
printf "Total Swapped Memory: %14u KB\n" $overall
该脚本是不言自明的。让我们在终端中运行它:
$ ./swp.sh
1658 Discord 41448 KB
1681 Xwayland 15772 KB
18621 firefox 213204 KB
18872 WebExtensions 60912 KB
...
Total Swapped Memory: 1177212 KB
我们可以通过使用column 工具之类的工具轻松地进一步自定义输出。
3. 使用smem实用程序
** smem工具显示进程的内存使用情况。除了显示 RSS、PSS 和 USS 内存外,它还可以显示交换内存。**
默认情况下,它不随大多数 Linux 发行版一起提供。因此,我们必须从发行版的官方存储库中安装它。
3.1. 安装
smem实用程序将在smem包名称下可用。我们可以使用yum或apt 之类的包管理器来安装它。
对于 Debian 及其衍生产品:
# apt install smem
对于 Fedora、OpenSUSE 和 RHEL:
# yum install smem
3.2. 用法
安装smem后,我们可以在终端中试用:
$ smem
PID User Command Swap USS PSS RSS
494 hey swaybg -o DVI-I-1 -i /home/ 15136 4 86 2304
1665 hey /opt/discord/Discord --type 8800 0 96 820
589 hey /usr/lib/pulse/gsettings-he 1224 20 122 2372
37348 hey /bin/sh /usr/bin/android-st 204 164 180 1220
...
假设我们要打印按交换内存使用量降序排列的前 10 个进程。我们可以使用-s或–sort*选项来做到这一点*:
$ smem -s swap -r | head -n10
PID User Command Swap USS PSS RSS
18943 hey /usr/lib/firefox/firefox -c 385536 161836 165339 213496
18621 hey /usr/lib/firefox/firefox 225660 458788 479474 576556
1748 hey /opt/discord/Discord --type 84652 130220 137640 153064
1658 hey /opt/discord/Discord 76108 28336 34916 51324
18872 hey /usr/lib/firefox/firefox -c 65172 133824 135810 169480
33599 hey /usr/lib/firefox/firefox -c 55012 261264 266269 324880
20578 hey megasync 52252 13272 15308 24040
35605 hey /usr/lib/firefox/firefox -c 46584 504264 507708 554040
1705 hey /opt/discord/Discord --type 41236 13876 18735 29988
-r选项将 反转输出,以降序打印列表。