pmap
ldd
stace
要熟悉的几个调试命令!
两个查看内存的文件
$ cat /proc/<pid>/stat | awk '{print $23 / 1024}' $ cat /proc/<pid>/status | grep -i vmsize
其中,关于内存的是 VSZ 和 RSS 这两个概念
man ps 看看它们的含义:
rss RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).
vsz VSZ virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject to change. (alias vsize).
观察到系统中的指定进程的地址空间分布情况显示一个目标文件或者链接库文件中的目标文件的各个段的大小。该文件从/proc//maps中获得数据。
命令格式:
pmap -x/-d pid
范例:
[root@localhost security]# pmap -d 4993
4993: -bash
Address Kbytes Mode Offset Device Mapping
08047000 596 r-x-- 0000000000000000 003:00006 bash bash的代码段
080dc000 28 rwx-- 0000000000094000 003:00006 bash bash的数据段
080e3000 280 rwx-- 00000000080e3000 000:00000 [ anon ] bash的堆
4d575000 84 r-x-- 0000000000000000 003:00006 ld-2.3.4.so 共享库的代码段
4d58a000 4 r-x-- 0000000000015000 003:00006 ld-2.3.4.so 共享库的数据段
4d58b000 4 rwx-- 0000000000016000 003:00006 ld-2.3.4.so 共享库的堆
4d58e000 1164 r-x-- 0000000000000000 003:00006 libc-2.3.4.so
4d6b1000 4 r-x-- 0000000000123000 003:00006 libc-2.3.4.so
4d6b2000 12 rwx-- 0000000000124000 003:00006 libc-2.3.4.so
4d6b5000 8 rwx-- 000000004d6b5000 000:00000 [ anon ] 匿名物理内存,
4d6de000 8 r-x-- 0000000000000000 003:00006 libdl-2.3.4.so
4d6e0000 8 rwx-- 0000000000001000 003:00006 libdl-2.3.4.so
4d807000 12 r-x-- 0000000000000000 003:00006 libtermcap.so.2.0.8
4d80a000 4 rwx-- 0000000000002000 003:00006 libtermcap.so.2.0.8
b7bc2000 176 r-x-- 0000000000000000 003:00006 GB18030.so
b7bee000 8 rwx-- 000000000002b000 003:00006 GB18030.so
b7bf0000 24 r-xs- 0000000000000000 003:00006 gconv-modules.cache
b7bf6000 1060 r-x-- 0000000002197000 003:00006 locale-archive
b7cff000 2048 r-x-- 0000000000000000 003:00006 locale-archive
b7eff000 36 r-x-- 0000000000000000 003:00006 libnss_files-2.3.4.so
b7f08000 8 rwx-- 0000000000008000 003:00006 libnss_files-2.3.4.so
b7f0a000 8 rwx-- 00000000b7f0a000 000:00000 [ anon ]
b7f20000 8 rwx-- 00000000b7f20000 000:00000 [ anon ]
bff0d000 84 rw--- 00000000bff0d000 000:00000 [ stack ]
ffffe000 4 ----- 0000000000000000 000:00000 [ anon ]
mapped: 5680K writeable/private: 460K shared: 24K
每列的含义如下:
参数 解释
Address:00378000-0038d000 进程所占的地址空间
Kbytes 该虚拟段的大小
RSS 设备号(主设备:次设备)
Anon 设备的节点号,0表示没有节点与内存相对应
Locked 是否允许swapped
Mode 权限:r=read, w=write, x=execute, s=shared, p=private(copy on write)
Mapping: bash 对应的映像文件名
循环显示进程3066的设备格式的最后1行,间隔2秒,
[root@C44 ~]# while true; do pmap -d 3066 | tail -1; sleep 2; done
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
mapped: 5412K writeable/private: 2028K shared: 0K
内存泄露脚本:
memleak.sh:
#!/bin/sh
i=1until [ ! "$i" -le 10 ]
do
val=$(pidof converter)
if [ -z "$val" ]; then
ret=$(pmap -d $val | tail -1)
fi
mytime=$(date "+%Y-%m-%d %H:%M:%S")
filetime=$(date "+%Y-%m-%d")
echo "[$mytime] [$ret]" >> memleak_$filetime.leak
sleep 5
done