我怎么知道哪个进程正在使用交换?
How can I know which process is using swap?
我的 fedora 盒子里有很多内存可用(大约 4G)但是交换空间被使用了(200+M)。
我想知道哪个进程正在使用交换。我怎么知道。
ps和top只显示内存使用情况。
提前致谢。
来自here:
[a] /proc/meminfo - This file reports statistics about memory usage on
the system. It is used by free to report the amount of free and used
memory (both physical and swap) on the system as well as the shared
memory and buffers used by the kernel. You can also use free, vmstat
and other tools to find out the same information.
[b]
/proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use
these files to find information about memory, pages and swap used by
each process using its PID.
[c] smem - This command (python script) reports memory usage with
shared memory divided proportionally.
也可以参考Find out what is using your swap
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print }'`
do
let SUM=$SUM+$SWAP
done
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL"
在 /proc/'processPID'/status
上,您可以在字段 VmSwap
上找到该信息。
使用此命令,您可以列出所有正在使用交换的进程。
for file in /proc/*/status ;
do
awk '/VmSwap|Name/{printf " " }END{ print ""}' $file;
done
参考:http://www.cyberciti.biz/faq/linux-which-process-is-using-swap/
改进 cyberciti.biz 命令以显示更简洁的答案:
(echo "COMM PID SWAP"; for file in /proc/*/status ; do awk '/^Pid|VmSwap|Name/{printf " " }END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r) | column -t
示例输出:
COMM PID SWAP
dockerd 662 2736 kB
skypeforlinux 26865 1320 kB
NetworkManager 303 1112 kB
slim 392 1028 kB
redis-server 350 204 kB
我对使用 awk 输出按交换使用排序的 table 的看法:
awk 'function pr(){if (s ~ /^[1-9]/) print p,n,s;n="";p="";s=""}BEGIN{FS="\t *";OFS="\t"}/^Name:/{pr();n=}/^VmSwap:/{s=}/^Pid:/{p=}END{pr()}' /proc/*/status | sort -t $'\t' -k3 -n -r | column -t -s $'\t'
示例输出:
33992 httpd 13916 kB
9331 httpd 10616 kB
43124 httpd 1800 kB
31353 httpd 592 kB
8592 master 184 kB
8606 crond 44 kB
8653 mingetty 40 kB
8655 mingetty 32 kB
基于 onadrianlzt 的回答。
我的 fedora 盒子里有很多内存可用(大约 4G)但是交换空间被使用了(200+M)。
我想知道哪个进程正在使用交换。我怎么知道。
ps和top只显示内存使用情况。
提前致谢。
来自here:
[a] /proc/meminfo - This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel. You can also use free, vmstat and other tools to find out the same information.
[b] /proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use these files to find information about memory, pages and swap used by each process using its PID.
[c] smem - This command (python script) reports memory usage with shared memory divided proportionally.
也可以参考Find out what is using your swap
#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print }'`
do
let SUM=$SUM+$SWAP
done
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL"
在 /proc/'processPID'/status
上,您可以在字段 VmSwap
上找到该信息。
使用此命令,您可以列出所有正在使用交换的进程。
for file in /proc/*/status ;
do
awk '/VmSwap|Name/{printf " " }END{ print ""}' $file;
done
参考:http://www.cyberciti.biz/faq/linux-which-process-is-using-swap/
改进 cyberciti.biz 命令以显示更简洁的答案:
(echo "COMM PID SWAP"; for file in /proc/*/status ; do awk '/^Pid|VmSwap|Name/{printf " " }END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r) | column -t
示例输出:
COMM PID SWAP
dockerd 662 2736 kB
skypeforlinux 26865 1320 kB
NetworkManager 303 1112 kB
slim 392 1028 kB
redis-server 350 204 kB
我对使用 awk 输出按交换使用排序的 table 的看法:
awk 'function pr(){if (s ~ /^[1-9]/) print p,n,s;n="";p="";s=""}BEGIN{FS="\t *";OFS="\t"}/^Name:/{pr();n=}/^VmSwap:/{s=}/^Pid:/{p=}END{pr()}' /proc/*/status | sort -t $'\t' -k3 -n -r | column -t -s $'\t'
示例输出:
33992 httpd 13916 kB
9331 httpd 10616 kB
43124 httpd 1800 kB
31353 httpd 592 kB
8592 master 184 kB
8606 crond 44 kB
8653 mingetty 40 kB
8655 mingetty 32 kB
基于 onadrianlzt 的回答。