Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm looking for a way, from a shell script, to get the swap usage for a particular process. Top can show this information, but only in interactive mode. Batch mode will only show customizations when a .toprc is present, but you can't specify a specific .toprc on the command-line. I found this script, but there are no swap entries in /proc/*/smaps on my server:

# find /proc/ -maxdepth 1 -type d -regex '^/proc/[0-9]+$' -exec grep -Hi swap '{}'/smaps \;

so the script just outputs 0 for every process.

Is there a way to find this information? I'm on RHEL5.1 2.6.18-53.el5 x86_64.


To be clear, when I ask about "swap usage", I mean the amount of memory used by a process that has been paged out to the swap filesystem (what is not currently sitting in main memory). Assuming no better way, I'm going to default to calculating it with (virtual - rss) via the values from ps -o vsz,rss.

share|improve this question
Well, hold on a second--are you saying there are no entries, i.e. it outputs a blank line and that's it? Or are you seeing Swap: 0 kB? Because this means they're not swapped--which is a valid state. If you're expecting things to be swapped out, what mechanism are you using to find this out? –  Andrew M. Apr 11 '12 at 20:28
i mean there are no entries. if there were entries, then the find command in my question would have displayed the 'Swap: 0'. nothing is displayed; there are no "Swap:" entries at all in /proc. –  Christopher Neylan Apr 11 '12 at 20:36
Your question says: so it just outputs 0 for every process, so I was just verifying. You may want to update your question to be clear that it doesn't output anything. –  Andrew M. Apr 11 '12 at 20:38
updated to say "the script". i thought "there are no swap entries in /proc/*/smaps" was clear enough :) –  Christopher Neylan Apr 11 '12 at 20:39
That's true; but what if you were seeing Swap: 0 kB and were assuming that it wasn't working? This was not the case here--but you'd probably be surprised about how often things are misinterpreted. :) Always better to be clear! –  Andrew M. Apr 11 '12 at 20:40
add comment

3 Answers

up vote 3 down vote accepted

An interesting question that had me scratching my head for a while.

First, why you have no Swap line: though in theory that was added in kernel 2.6.16, it didn't show up in RHEL until 5.3 or so (kernel 2.6.18-99.el5). Here is the commit (unfortunately not accessible in the RH Bugzilla; if you happen to have an account with appropriate privileges, the Bugzilla id is 443749). http://sophie2.aero.jussieu.fr/distrib/Scientific%20Linux/5x/i386/by-pkgid/351d529f9beeb4e5d936a6d5e3e7813a/files/645

Second, this implies that the top data is unreliable since it is not reading from the smaps (i.e. it is virt - res):

    case P_SWP:
       MKCOL(scale_num(PAGES_TO_KB(p->size - p->resident), w, s));

Third, I regret to say that the kernel is the One True Oracle on how much swap is used, and as far as I can tell the various patches to make the information available in smaps and status are the only way to expose that information. Unfortunately, I see updating your kernel as the only way to get accurate information; either by patching the source yourself (sample patch, no warranty) or updating to kernel 2.6.18-99.el5.

I wish there were an easier answer; blame Linus.

share|improve this answer
sigh, as i feared. thanks for your effort, though. +1 for top source. –  Christopher Neylan Apr 12 '12 at 15:10
add comment

Look in /proc/<pid>/status. You'll see a VmSwap line.

share|improve this answer
there are no such entries in my /proc/*/status files. (verified with find /proc/ -maxdepth 1 -type d -regex '^/proc/[0-9]+$' -exec grep -Hi swap '{}'/status \;). missing just like the swap entries that should be in /proc/*/smaps. –  Christopher Neylan Apr 11 '12 at 20:43
Is this a virtual machine? If so, it may not have any "swap", only the physical machine does. (It would be inefficient for a virtual machine to virtually swap to a virtual page file when the physical machine can manage memory as one large pool and avoid having to do that.) –  David Schwartz Apr 11 '12 at 20:48
nope, physical host. in any case, would top report swap usage even if there were no swap? –  Christopher Neylan Apr 11 '12 at 20:51
Yes. And the status file will have a VmSwap entry too, even if there is no swap. (Assuming swap is enabled in the kernel.) –  David Schwartz Apr 11 '12 at 20:52
+1 for an answer that works in kernels newer than mine :) –  Christopher Neylan Apr 12 '12 at 15:11
add comment

It looks like from the ps(1) man page for CentOS 5(1) that you can specify vsize(VSZ) as an output field, so would something like:

/bin/ps -p $pid -o vsize=

do the trick for you? That extracts the "virtual memory size of the process in KiB (1024-byte units)" and the = makes it leave off the column header.

share|improve this answer
virtual memory is not the same as swapped memory. some say that (virtual - rss =~ swap), but i remember reading somewhere that that calculation is inaccurate (for linux memory stat standards). using (virt - rss) is a fallback for me right now. –  Christopher Neylan Apr 11 '12 at 20:23
He's looking for swap usage, not virtual memory size. –  David Schwartz Apr 11 '12 at 20:38
Okay, it sounds like you have already tried this technique. Thanks for thoughtful response and sorry I wasn't more help. –  adric Apr 11 '12 at 20:51
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.