Tux

...making Linux just a little more fun!

2-cent Tip: Checking the amount of swapped out memory owned by a process

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sat, 6 Jun 2009 01:15:58 +0700

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.

regards,

Mulyadi.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 15 Jun 2009 12:24:01 -0500

> From: Mulyadi Santosa <mulyadi.santosa@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@Jotunheim:~$ swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda5                               partition       2996080 2320    -1

### Top 10 swap hogs, roughly
ben@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@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@Jotunheim:~$ awk -v name="$n" '/^Swap/{if ($2>0)print $2}' /proc/[0-9]*/smaps
ben@Jotunheim:~$ 

Sorry, Mulyadi - that one didn't pan out.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


David Richardson [dsrich at ieee.org]


Mon, 15 Jun 2009 12:40:29 -0400

Ben Okopnik wrote:

> Sorry, Mulyadi - that one didn't pan out.

Doesn't work under Ubuntu 8.10, either.

-- 
David Richardson   \   Imagine Whirled Peas
dsrich@ieee.org     \
These are my opinions - nobody else wants them.


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Tue, 16 Jun 2009 15:30:39 +0700

Hmmm....I began to think that this swap accounting is only included in Fedora kernel tree only....

Sadly, I only have Fedora installation here to test. If I have time, I'll see if this swap accounting patch is already merged in mainline kernel.

regards,

Mulyadi.


Top    Back