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

by on November 20, 2007 · 20 comments· LAST UPDATED November 20, 2007

in , ,

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       -       -       -
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 20 comments… read them below or add one }

1 Manoj November 21, 2007 at 3:25 am

-x option not found in redhat

Reply

2 Anonymous September 9, 2011 at 3:30 pm

Suse Enterprise Linux 10SP1 also lacks the -x option.

Reply

3 nixCraft November 21, 2007 at 5:51 am

Manoj,

You must be using older version update procps package.

Reply

4 jj November 24, 2007 at 5:57 pm

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

Reply

5 BT November 25, 2007 at 6:02 am

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

Reply

6 Nate November 28, 2007 at 8:58 pm

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

Reply

7 Nate November 28, 2007 at 8:59 pm

pmap -x is what I meant.

Reply

8 spice February 11, 2008 at 8:48 pm

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.

Reply

9 Helal Uddin March 10, 2008 at 3:52 am

thanks a lot for helping me.

Reply

10 siva April 1, 2008 at 10:23 am

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

Reply

11 Adam July 3, 2008 at 5:47 pm

I am trying to find out why my Apache is using a massive amount of memory for simple usage. Then it starts using swap space even after only few requests. I am going to try to use this command to find out what Apache is using the memory for. I’m not sure if it is a memory leak and this tool might not help me, but I will find out.

Reply

12 Raghunatha Reddy March 11, 2009 at 5:04 am

i am using redhat linux 5.2 with UniVerse database. from last 20 day my server physical memory and CPU usage is 99% and 100% . i tried lot to resolve this problem . but i am not able to fix this problem. can any body help me. i am sending the out put of free -m and ipcs out put . server running in Runlevel 5
total used free shared buffers cached
Mem: 7741 7691 50 0 120 7244
-/+ buffers/cache: 326 7415
Swap: 1983 0 1983
#ipcs
—— Shared Memory Segments ——–
key shmid owner perms bytes nattch status
0xaceca200 32768 root 666 6852808 9
0x00000000 229377 gdm 600 393216 2 dest
0xaceb000e 64258051 tcsuser 600 10240 1
0xaceb000f 64290822 tcsuser 600 10240 1
0xaceb000c 64323591 tcsuser 600 10240 1
0xaceb0013 64356360 tcsuser 600 10240 1
0xacebdf50 73760777 jyg5506 600 10240 1
0xaceb0010 64389130 tcsuser 600 10240 1
0xaceb0017 64421900 tcsuser 600 10240 1
0xaceb0015 64454669 tcsuser 600 10240 1

—— Semaphore Arrays ——–
key semid owner perms nsems

—— Message Queues ——–
key msqid owner perms used-bytes messages
0xacea0207 0 root 666 0 0

#vmstat 2 5
procs ———–memory———- —swap– —–io—- –system– —–cpu—– –
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 656 50988 123184 7418868 0 0 2584 146 4 9 13 4 80 3 0
2 0 656 44864 123152 7415656 0 0 400 6 1056 132 86 14 0 1 0
1 0 656 53200 123160 7416428 0 0 406 178 1064 150 87 13 0 1 0
1 0 656 47832 123184 7417196 0 0 406 46 1053 134 85 14 0 2 0
1 0 656 47236 123184 7417960 0 0 366 16 1053 128 86 13 0 1 0

Reply

13 kiran June 21, 2009 at 6:56 am

I think u must check on the netstat command also

Reply

14 Anonymous May 25, 2010 at 7:58 am

This prints process memory usage:
ps -p -o size=

Reply

15 Anonymous June 8, 2010 at 12:56 pm

Can it be possible like shhoting the cpu usage to around 600%, the one we see while giving top command?

Reply

16 tessa rodriguez November 23, 2011 at 8:55 am

let me know how to down load any app i wont to own acer500

Reply

17 阿铭linux February 22, 2012 at 7:08 am

use pmap -d pid
the last line displays the sizes:
what’s the meaning of share?

Reply

18 sheetal May 9, 2012 at 1:20 pm

how to calculate the memory of any progam?

Reply

19 sheetal May 9, 2012 at 1:21 pm

plz reply soon…. how to calculate the memory used by any program?

Reply

20 Pradeep H N July 24, 2012 at 11:56 am

you can find the memory with the help of TOP command,
what is the version of Linux you are using

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous post:

Next post: