pmap–linux查看进程占用内存
日常工作中,我们最常用查看某个进程占用的内存数大小的工具应该就是ps了。首先,我们现用ps看看某一个mysql实例的内存占用情况。
1 2 3 4 | USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND .... mysql 9746 62.7 26.5 9507852 8713056 ? SLl Jul25 32995:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/database/mysql/ --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/data/database/mysql//zoo28.err --open-files-limit=10240 --pid-file=/data/database/mysql//zoo28.pid --socket=/tmp/mysql.sock --port=3306 .... |
这里我们暂且只关注两列:VSZ:9507852,RSS:8713056。关于这两个参数,看看man中的定义是:
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).
简单一点说,RSS 就是这个process 实际占用的物理内存,VSZ 就是process 的虚拟内存,就是process 现在没有使用但未来可能会分配的内存大小。一般情况下,VSZ会略大于RSS,如果VSZ远大于RSS时说明系统存在很多没能正常回收的内存碎片,如果RSS远大于VSZ说明内存不够分配,需要大量使用swap交换内存。注意,这里的RSS值实际上是包含了这个进程的共享内存,并不准确,如果我们想知道这个进程真正分配到的内存大小,可以用pmap这个命令来获取。
pmap的用法很简单,直接pmap [ -x | -d ] [ -q ] pids…
选项含义
-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. 显示版本
扩展格式和设备格式域:
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) 设备名
这里我们只想看这个mysql占用多少内存不看详情,因此只输出最后一行:
1 2 | [root@sy-mm_log_xxx ~]# pmap -d 9746 | tail -n 1 mapped: 9508032K writeable/private: 9464048K shared: 0K |
mapped 表示该进程映射的虚拟地址空间大小,也就是该进程预先分配的虚拟内存大小,即ps出的vsz
writeable/private 表示进程所占用的私有地址空间大小,也就是该进程实际使用的内存大小
shared 表示进程和其他进程共享的内存大小
这里可以看到这个实例大概是占用了9G左右的内存。
最后分享一句命令,用来循环监控输出某个命令:
1 2 3 4 5 6 | [root@sy-mm_log_xxx ~]# while true;do pmap -d 16528 | tail -n 1;sleep 2;done mapped: 3427856K writeable/private: 3416536K shared: 0K mapped: 3427856K writeable/private: 3416536K shared: 0K mapped: 3427856K writeable/private: 3416536K shared: 0K mapped: 3427856K writeable/private: 3416536K shared: 0K mapped: 3427856K writeable/private: 3416536K shared: 0K |
参考文章:
Redis设置Key的过期时间 – EXPIRE命令 记录:secureCRT设置会话自动加载VIM配置文件