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 - - -
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 20 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
you can find the memory with the help of TOP command,
what is the version of Linux you are using
plz reply soon…. how to calculate the memory used by any program?
how to calculate the memory of any progam?
use pmap -d pid
the last line displays the sizes:
what’s the meaning of share?
let me know how to down load any app i wont to own acer500
This prints process memory usage:
ps -p -o size=
Can it be possible like shhoting the cpu usage to around 600%, the one we see while giving top command?
I think u must check on the netstat command also
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
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.
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
thanks a lot for helping me.
pmap -x is what I meant.
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
You must be root to use that command.
It works on ubuntu if you sudo pmap -x pid.
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
Manoj,
You must be using older version update procps package.
-x option not found in redhat
Suse Enterprise Linux 10SP1 also lacks the -x option.