Wednesday, 29 July 2009

Linux find the memory used by a program / process

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 - - -

No comments: