Polls

Topics

Linux find the memory used by a program / process using pmap command

Posted by Vivek on Tuesday November 20, 07 @5:00 pm

You can find the memory used by a program (process) by looking into /proc directory or using standard command such as ps or top. However, you must calculate all memory usage by hand i.e. add Shared Memory + mapped file + total virtual memory size of the process + Resident Set Size + non-swapped physical memory used by process.

So how do you find the memory used by a process or program under Linux? Use a tool called pmap. It reports the memory map of a process or processes.

pmap examples

To display process mappings, type
$ pmap pid
$ pmap 3724

Output:

3724:   /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
0000000000400000    164K r-x--  /usr/sbin/lighttpd
0000000000629000     12K rw---  /usr/sbin/lighttpd
000000000bb6b000   4240K rw---    [ anon ]
00000035ee600000    104K r-x--  /lib64/ld-2.5.so
00000035ee819000      4K r----  /lib64/ld-2.5.so
00000035ee81a000      4K rw---  /lib64/ld-2.5.so
00000035eea00000   1304K r-x--  /lib64/libc-2.5.so
00000035eeb46000   2048K -----  /lib64/libc-2.5.so
00000035eed46000     16K r----  /lib64/libc-2.5.so
00000035eed4a000      4K rw---  /lib64/libc-2.5.so
00000035eed4b000     20K rw---    [ anon ]
00000035eee00000      8K r-x--  /lib64/libdl-2.5.so
00000035eee02000   2048K -----  /lib64/libdl-2.5.so
.....
....
00002aaaac51e000      4K r----  /lib64/libnss_files-2.5.so
00002aaaac51f000      4K rw---  /lib64/libnss_files-2.5.so
00007fff7143b000     84K rw---    [ stack ]
ffffffffff600000   8192K -----    [ anon ]
 total            75180K

The -x option can be used to provide information about the memory allocation and mapping types per mapping. The amount of resident, non-shared anonymous, and locked memory is shown for each mapping:
pmap -x 3526
Output:

3526:   -bash
Address           Kbytes     RSS    Anon  Locked Mode   Mapping
0000000000400000     700       -       -       - r-x--  bash
00000000006ae000      40       -       -       - rw---  bash
00000000006b8000      20       -       -       - rw---    [ anon ]
00000000008b7000      32       -       -       - rw---  bash
00000000098de000     536       -       -       - rw---    [ anon ]
00000035ee600000     104       -       -       - r-x--  ld-2.5.so
00000035ee819000       4       -       -       - r----  ld-2.5.so
00000035ee81a000       4       -       -       - rw---  ld-2.5.so
00000035eea00000    1304       -       -       - r-x--  libc-2.5.so
00000035eeb46000    2048       -       -       - -----  libc-2.5.so
00000035eed46000      16       -       -       - r----  libc-2.5.so
00000035eed4a000       4       -       -       - rw---  libc-2.5.so
00000035eed4b000      20       -       -       - rw---    [ anon ]
00000035eee00000       8       -       -       - r-x--  libdl-2.5.so
00000035eee02000    2048       -       -       - -----  libdl-2.5.so
00000035ef002000       4       -       -       - r----  libdl-2.5.so
00000035ef003000       4       -       -       - rw---  libdl-2.5.so
00000035ef600000      12       -       -       - r-x--  libtermcap.so.2.0.8
00000035ef603000    2044       -       -       - -----  libtermcap.so.2.0.8
00000035ef802000       4       -       -       - rw---  libtermcap.so.2.0.8
00002aaaaaaab000       4       -       -       - rw---    [ anon ]
00002aaaaaaba000      12       -       -       - rw---    [ anon ]
00002aaaaaabd000      40       -       -       - r-x--  libnss_files-2.5.so
00002aaaaaac7000    2044       -       -       - -----  libnss_files-2.5.so
00002aaaaacc6000       4       -       -       - r----  libnss_files-2.5.so
00002aaaaacc7000       4       -       -       - rw---  libnss_files-2.5.so
00002aaaaacc8000   55112       -       -       - r----  locale-archive
00002aaaae29a000      28       -       -       - r--s-  gconv-modules.cache
00002aaaae2a1000       8       -       -       - rw---    [ anon ]
00007fff9bff4000      92       -       -       - rw---    [ stack ]
ffffffffff600000    8192       -       -       - -----    [ anon ]
----------------  ------  ------  ------  ------
total kB           74496       -       -       -

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in...

Discussion on This Article:

  1. Manoj Says:

    -x option not found in redhat

  2. vivek Says:

    Manoj,

    You must be using older version update procps package.

  3. jj Says:

    I believe what you are saying is you need to first establish the PID and then use pmap. I found a command ps -C syslogd -o pid=firefox that can be used to find memory usage of firefox and thought I’d pass it along.

    P.S. the pmap -x perameter works in Ubuntu

  4. BT Says:

    You must be root to use that command.
    It works on ubuntu if you sudo pmap -x pid.

  5. Nate Says:

    Linux version 2.6.9-55.0.9.ELsmp (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)) #1 SMP Tue Sep 25 02:17:24 EDT 2007

    Usage: pmap [-x | -d] [-q] pid…
    -x show details
    -d show offset and device number
    -q quiet; less header/footer info
    -V show the version number

    pmap -v exists in RHEL4

  6. Nate Says:

    pmap -x is what I meant.

  7. spice Says:

    I found a command ps -C syslogd -o pid=firefox that can be used to find memory usage of firefox and thought I’d pass it along.>

    That command gives the process id of syslogd, not memory usage of anything.

  8. Helal Uddin Says:

    thanks a lot for helping me.

  9. siva Says:

    hi!
    every thing seems good, but how to calculate memory used and how top commnad and free are showing memory used, how they are calculating and showing the information.
    thanks and regards
    siva

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , ~ Last updated on: November 20, 2007

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.