Linux之pmap簡介 一、pmap簡介 (1)名稱: pmap - report memory map of a process(查看進程的記憶體映像資訊) (2)用法 pmap [ -x | -d ] [ -q ] pids... pmap -V (3)選項含義 -x extended Show the extended format. 顯示擴充格式 -d device Show the device format. 顯示裝置格式 -q quiet Do not display some header/footer lines. 不顯示頭尾行 -V show version Displays version of program. 顯示版本 (4)擴充格式和裝置格式域 Address: start address of map 映像起始地址 Kbytes: size of map in kilobytes 映像大小 RSS: resident set size in kilobytes 駐留集大小 Dirty: dirty pages (both shared and private) in kilobytes 髒頁大小 Mode: permissions on map 映像許可權: r=read, w=write, x=execute, s=shared, p=private (copy on write) Mapping: file backing the map , or '[ anon ]' for allocated memory, or '[ stack ]' for the program stack. 映像支援檔案,[anon]為已指派記憶體 [stack]為程式堆棧 Offset: offset into the file 檔案位移 Device: device name (major:minor) 裝置名稱 二、應用舉例 [xxx@xxx ~]# pmap -d 21147 21147: ./ogassistservice Address Kbytes Mode Offset Device Mapping 0000000000400000 360 r-x-- 0000000000000000 003:00002 ogassistservice 000000000065a000 4 rw--- 000000000005a000 003:00002 ogassistservice 000000000065b000 128 rw--- 000000000065b000 000:00000 [ anon ] 0000000011232000 523804 rw--- 0000000011232000 000:00000 [ anon ] 0000003304000000 12 r-x-- 0000000000000000 003:00002 libuuid.so.1.2 0000003304003000 2048 ----- 0000000000003000 003:00002 libuuid.so.1.2 0000003304203000 4 rw--- 0000000000003000 003:00002 libuuid.so.1.2 000000376d000000 920 r-x-- 0000000000000000 003:00002 libstdc++.so.6.0.8 000000376d0e6000 2044 ----- 00000000000e6000 003:00002 libstdc++.so.6.0.8 000000376d2e5000 24 r---- 00000000000e5000 003:00002 libstdc++.so.6.0.8 000000376d2eb000 12 rw--- 00000000000eb000 003:00002 libstdc++.so.6.0.8 000000376d2ee000 72 rw--- 000000376d2ee000 000:00000 [ anon ] 0000003bc5800000 112 r-x-- 0000000000000000 003:00002 ld-2.5.so 0000003bc5a1b000 4 r---- 000000000001b000 003:00002 ld-2.5.so 0000003bc5a1c000 4 rw--- 000000000001c000 003:00002 ld-2.5.so 0000003bc5c00000 1336 r-x-- 0000000000000000 003:00002 libc-2.5.so 0000003bc5d4e000 2048 ----- 000000000014e000 003:00002 libc-2.5.so 0000003bc5f4e000 16 r---- 000000000014e000 003:00002 libc-2.5.so 0000003bc5f52000 4 rw--- 0000000000152000 003:00002 libc-2.5.so 0000003bc5f53000 20 rw--- 0000003bc5f53000 000:00000 [ anon ] 0000003bc6000000 8 r-x-- 0000000000000000 003:00002 libdl-2.5.so 0000003bc6002000 2048 ----- 0000000000002000 003:00002 libdl-2.5.so 0000003bc6202000 4 r---- 0000000000002000 003:00002 libdl-2.5.so 0000003bc6203000 4 rw--- 0000000000003000 003:00002 libdl-2.5.so 0000003bc6400000 520 r-x-- 0000000000000000 003:00002 libm-2.5.so 0000003bc6482000 2044 ----- 0000000000082000 003:00002 libm-2.5.so 0000003bc6681000 4 r---- 0000000000081000 003:00002 libm-2.5.so 0000003bc6682000 4 rw--- 0000000000082000 003:00002 libm-2.5.so 0000003bc6800000 88 r-x-- 0000000000000000 003:00002 libpthread-2.5.so 0000003bc6816000 2044 ----- 0000000000016000 003:00002 libpthread-2.5.so 0000003bc6a15000 4 r---- 0000000000015000 003:00002 libpthread-2.5.so 0000003bc6a16000 4 rw--- 0000000000016000 003:00002 libpthread-2.5.so 0000003bc6a17000 16 rw--- 0000003bc6a17000 000:00000 [ anon ] 0000003bc6c00000 80 r-x-- 0000000000000000 003:00002 libz.so.1.2.3 0000003bc6c14000 2044 ----- 0000000000014000 003:00002 libz.so.1.2.3 0000003bc6e13000 4 rw--- 0000000000013000 003:00002 libz.so.1.2.3 0000003bc7800000 28 r-x-- 0000000000000000 003:00002 librt-2.5.so 0000003bc7807000 2048 ----- 0000000000007000 003:00002 librt-2.5.so 0000003bc7a07000 4 r---- 0000000000007000 003:00002 librt-2.5.so 0000003bc7a08000 4 rw--- 0000000000008000 003:00002 librt-2.5.so 0000003bc7c00000 52 r-x-- 0000000000000000 003:00002 libgcc_s-4.1.2-20080825.so.1 0000003bc7c0d000 2048 ----- 000000000000d000 003:00002 libgcc_s-4.1.2-20080825.so.1 0000003bc7e0d000 4 rw--- 000000000000d000 003:00002 libgcc_s-4.1.2-20080825.so.1 00002b7b46c54000 20 rw--- 00002b7b46c54000 000:00000 [ anon ] 00002b7b46c5a000 4 rw--- 00002b7b46c5a000 000:00000 [ anon ] 00002b7b46c66000 260 r-x-- 0000000000000000 003:00004 libevent.so 00002b7b46ca7000 2048 ----- 0000000000041000 003:00004 libevent.so 00002b7b46ea7000 8 rw--- 0000000000041000 003:00004 libevent.so 00002b7b46ea9000 4 rw--- 00002b7b46ea9000 000:00000 [ anon ] 00002b7b46eaa000 968 r-x-- 0000000000000000 003:00004 libprotobuf.so 00002b7b46f9c000 2044 ----- 00000000000f2000 003:00004 libprotobuf.so 00002b7b4719b000 24 rw--- 00000000000f1000 003:00004 libprotobuf.so 00002b7b471a1000 504 r-x-- 0000000000000000 003:00004 liblog4cplus.so 00002b7b4721f000 2048 ----- 000000000007e000 003:00004 liblog4cplus.so 00002b7b4741f000 24 rw--- 000000000007e000 003:00004 liblog4cplus.so 00002b7b47425000 48 r-x-- 0000000000000000 003:00004 librabbitmq.so 00002b7b47431000 2044 ----- 000000000000c000 003:00004 librabbitmq.so 00002b7b47630000 4 rw--- 000000000000b000 003:00004 librabbitmq.so 00002b7b47631000 1048 rw--- 00002b7b47631000 000:00000 [ anon ] 00007fffd6054000 84 rw--- 00007ffffffe9000 000:00000 [ stack ] 00007fffd61ac000 16 r-x-- 00007fffd61ac000 000:00000 [ anon ] ffffffffff600000 8192 ----- 0000000000000000 000:00000 [ anon ] mapped: 565476K writeable/private: 525312K shared: 0K 上面這一堆中,最後一行的值必須知道: mapped 表示該進程映射的虛擬位址空間大小,也就是該進程預先分配的虛擬記憶體大小,即ps出的vsz writeable/private 表示進程所佔用的私人地址空間大小,也就是該進程實際使用的記憶體大小 shared 表示進程和其他進程共用的記憶體大小 三、其它命令 除了pmap外,其實還有別的手段可以用於靜態查看某個進程的記憶體使用量情況: (1)ps ps -aux|grep process_name (2)cat /proc/process_id/status Name: xxx State: S (sleeping) SleepAVG: 98% Tgid: 21147 Pid: 21147 PPid: 18228 TracerPid: 0 Uid: 0 0 0 0 Gid: 0 0 0 0 FDSize: 131072 Groups: 0 1 2 6 10 VmPeak: 557288 kB VmSize: 557288 kB VmLck: 0 kB VmHWM: 527480 kB VmRSS: 527480 kB VmData: 525132 kB VmStk: 88 kB VmExe: 360 kB VmLib: 4936 kB VmPTE: 1140 kB StaBrk: 11232000 kB Brk: 311b9000 kB StaStk: 7fffd60678c0 kB Threads: 1 SigQ: 0/16383 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000001000 SigCgt: 0000000180000000 CapInh: 0000000000000000 CapPrm: 00000000fffffeff CapEff: 00000000fffffeff Cpus_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,7fffffff,ffffffff Mems_allowed: 00000000,00000001 上面這一堆中有兩個重要參數必須知道: VmSize:任務虛擬位址空間大小 VmRSS:應用程式正在使用的實體記憶體大小