6.9. Calculating Process Memory Usage with ps and pmapRecall that the memory use of a process can be categorized into two classes: its virtual memory usage and its physical memory usage (referred to as its resident set size, or RSS). The virtual memory size is the amount of virtual address space that has been allocated to the process, and the physical memory is the amount of real memory pages that has been allocated to a process. You use the ps command to display a process's virtual and physical memory usage. $ ps -eo pid,vsz,rss,args PID VSZ RSS COMMAND 11896 1040 736 ps -eo pid,vsz,rss,args 11892 1032 768 sh 3603 1032 768 sh 2695 1896 1432 telnet donan 2693 1920 1456 telnet donan 2433 1920 1440 telnet firefly 3143 1920 1456 telnet devnull 2429 1920 1440 telnet firefly.eng 2134 1920 1440 telnet devnull From the ps example, you see that the /bin/sh shell uses 1032 Kbytes of virtual memory, 768 Kbytes of which have been allocated from physical memory, and that two shells are running. ps reports that both shells are using 768 Kbytes of memory each, but in fact, because each shell uses dynamic shared libraries, the total amount of physical memory used by both shells is much less than 768K x 2. To ascertain how much memory is really being used by both shells, look more closely at the address space within each process. Figure 6.4 shows how the two shells share both the /bin/sh binary and their shared libraries. The figure shows each mapping of memory within the shell's address space. We've separated the memory use into three categories:
Figure 6.4. Process Private and Shared Mappings (/bin/sh Example)The pmap command displays every mapping within the process's address space, so you can inspect a process and estimate shared and private memory usage. The amount of resident, nonshared anonymous, and locked memory is shown for each mapping. sol9$ pmap -x 102908 102908: sh Address Kbytes Resident Anon Locked Mode Mapped File 00010000 88 88 - - r-x-- sh 00036000 8 8 8 - rwx-- sh 00038000 16 16 16 - rwx-- [ heap ] FF260000 16 16 - - r-x-- en_.so.2 FF272000 16 16 - - rwx-- en_US.so.2 FF280000 664 624 - - r-x-- libc.so.1 FF336000 32 32 8 - rwx-- libc.so.1 FF360000 16 16 - - r-x-- libc_psr.so.1 FF380000 24 24 - - r-x-- libgen.so.1 FF396000 8 8 - - rwx-- libgen.so.1 FF3A0000 8 8 - - r-x-- libdl.so.1 FF3B0000 8 8 8 - rwx-- [ anon ] FF3C0000 152 152 - - r-x-- ld.so.1 FF3F6000 8 8 8 - rwx-- ld.so.1 FFBFE000 8 8 8 - rw--- [ stack ] -------- ----- ----- ----- ------ total Kb 1072 1032 56 - The example output from pmap shows the memory map of the /bin/sh command. At the top of the output are the executable text and data mappings. All the executable binary is shared with other processes because it is mapped read-only into each process. A small portion of the data mapping is shared; some is private because of copy-on-write (COW) operations. You can estimate the amount of incremental memory used by each additional instance of a process by using the resident and anonymous memory counts of each mapping. In the above example, the Bourne shell has a resident memory size of 1032 Kbytes. However, a large amount of the physical memory used by the shell is shared with other instances of the shell. Another identical instance of the shell will share physical memory with the other shell where possible and will allocate anonymous memory for any nonshared portion. In the above example, each additional Bourne shell uses approximately 56 Kbytes of additional physical memory. A more complex example shows the output format for a process containing different mapping types. In this example, the mappings are as follows:
sol9$ pmap -x 15492 15492: ./maps Address Kbytes RSS Anon Locked Mode Mapped File 00010000 8 8 - - r-x-- maps 00020000 8 8 8 - rwx-- maps 00022000 20344 16248 16248 - rwx-- [ heap ] 03000000 1024 1024 - - rw-s- dev:0,2 ino:4628487 04000000 1024 1024 512 - rw--- dev:0,2 ino:4628487 05000000 1024 1024 512 - rw--R dev:0,2 ino:4628487 06000000 1024 1024 1024 - rw--- [ anon ] 07000000 512 512 512 - rw--R [ anon ] 08000000 8192 8192 - 8192 rwxs- [ dism shmid=0x5] 09000000 8192 4096 - - rwxs- [ dism shmid=0x4] 0A000000 8192 8192 - 8192 rwxsR [ ism shmid=0x2 ] 0B000000 8192 8192 - 8192 rwxsR [ ism shmid=0x3 ] FF280000 680 672 - - r-x-- libc.so.1 FF33A000 32 32 32 - rwx-- libc.so.1 FF390000 8 8 - - r-x-- libc_psr.so.1 FF3A0000 8 8 - - r-x-- libdl.so.1 FF3B0000 8 8 8 - rwx-- [ anon ] FF3C0000 152 152 - - r-x-- ld.so.1 FF3F6000 8 8 8 - rwx-- ld.so.1 FFBFA000 24 24 24 - rwx-- [ stack ] -------- ------- ------- ------- ------- total Kb 50464 42264 18888 16384 |