[TAG] [TIPS] checking the amount of swapped out memory owned by a process
Ben Okopnik
ben at linuxgazette.net
Mon Jun 15 21:24:01 MSD 2009
> ---------- Forwarded message ----------
> From: Mulyadi Santosa <mulyadi.santosa at gmail.com>
>
> Hi all
>
> Recent Linux kernel versions allow us to see how much memory owned by
> a process is swapped out. All you need to do is the PID of the process
> and grab the output of related /proc entry:
> $ cat /proc/<pid of your process>/smaps | grep Swap
>
> To easily sum up all these per-process swap output, simply use below awk script:
> $ cat /proc/<pid of your process>/smaps ?| grep Swap | awk '{ SUM +=
> $2 } END { print SUM }'
> the unit is in kilobyte.
>
> PS: This is confirmed in Fedora 9 using Linux kernel version
> 2.6.27.21-78.2.41.fc9.i686.
_Not_ confirmed in Ubuntu 9.04. Here's what my system looks like after
I've launched a whole bunch of Firefox and GIMP processes:
```
### Swap state
ben at Jotunheim:~$ swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2996080 2320 -1
'''
```
### Top 10 swap hogs, roughly
ben at Jotunheim:~$ ps hk-size -eo pid,size,cmd|head
17435 160344 /usr/lib/firefox-3.0.10/firefox
2853 36032 spamd child
3689 34724 /usr/lib/bonobo-activation/bonobo-activation-server --ac-activate --ior-output-fd=19
18392 33540 gimp
3687 29708 nautilus
3669 25264 /usr/lib/gvfs//gvfs-fuse-daemon /home/ben/.gvfs
2486 24240 /usr/sbin/spamd --create-prefs --max-children 5 --helper-home-dir -d --pidfile=/var/run/spamd.pid
13411 24240 spamd child
3777 20680 /usr/lib/gnome-applets/mixer_applet2 --oaf-activate-iid=OAFIID:GNOME_MixerApplet_Factory --oaf-ior-fd=26
3645 18180 /usr/bin/pulseaudio --start
'''
```
### Loop over the PIDs and sum up the swap lines;
### 'awk' invocation modified slightly to show PIDs.
ben at Jotunheim:~$ for n in `ps hk-size -eo size,pid|sed '11,$d;s/.* //'`
> do
> awk -v pid=$n '/^Swap/{a+=$1}END{printf "%-5d: %d\n",pid,a}' /proc/$n/smaps
> done
17435: 0
2853 : 0
3689 : 0
18392: 0
3687 : 0
3669 : 0
2486 : 0
13411: 0
3777 : 0
3645 : 0
'''
In fact, there's not a single process on the system that shows a 'Swap'
line that's greater than 0:
```
ben at Jotunheim:~$ awk -v name="$n" '/^Swap/{if ($2>0)print $2}' /proc/[0-9]*/smaps
ben at Jotunheim:~$
'''
Sorry, Mulyadi - that one didn't pan out.
--
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
More information about the TAG
mailing list