测试环境:Ubuntu14.04
1. 获取进程ID号
ps -aux | grep your_process_name
例如:
xxx@xxx:~$ ps -e |grep Midlet|awk '{print $1}'
9356
|
2. 查看进程相关信息
2.1 查看进程树信息:
xxx@xxx:~$ pstree -p 9356
runMidlet(9356)───{runMidlet}(9357)
|
2.2 查看进程打开的文件:
- xxx@xxx:~$ lsof -p 9356
- lsof: WARNING: can't stat() ext4 file system /var/lib/docker/aufs
- Output information may be incomplete.
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- runMidlet 9356 lubaoquan cwd DIR 8,5 4096 21096156 /home/lubaoquan/java/phoneme/temp/bin/i386
- runMidlet 9356 lubaoquan rtd DIR 8,1 4096 2 /
- runMidlet 9356 lubaoquan txt REG 8,5 2033266 20979964 /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet
- runMidlet 9356 lubaoquan mem REG 8,1 1754876 3682281 /lib/i386-linux-gnu/libc-2.19.so
- runMidlet 9356 lubaoquan mem REG 8,1 114372 3670186 /lib/i386-linux-gnu/libgcc_s.so.1
- runMidlet 9356 lubaoquan mem REG 8,1 134614 3682267 /lib/i386-linux-gnu/libpthread-2.19.so
- runMidlet 9356 lubaoquan DEL REG 0,5 3375128 /SYSV62010009
- runMidlet 9356 lubaoquan mem REG 8,1 134380 3682268 /lib/i386-linux-gnu/ld-2.19.so
- runMidlet 9356 lubaoquan 0u CHR 136,3 0t0 6 /dev/pts/3
- runMidlet 9356 lubaoquan 1u CHR 136,3 0t0 6 /dev/pts/3
- runMidlet 9356 lubaoquan 2u CHR 136,3 0t0 6 /dev/pts/3
- runMidlet 9356 lubaoquan 3r FIFO 8,1 0t0 1441801 /tmp/.qtvfb_mouse-0
- runMidlet 9356 lubaoquan 4r FIFO 8,1 0t0 1441800 /tmp/.qtvfb_keyboard-0
|
2.3 查看进程系统调用相关信息:
- xxx@xxx:~$ sudo strace -p 9356</p>Process 9356 attached
- [ Process PID=9356 runs in 32 bit mode. ]
- select(5, [3 4], [], [], {0, 244888}) = 0 (Timeout)
- select(5, [3 4], [], [], {0, 250000}) = 0 (Timeout)
- select(5, [3 4], [], [], {0, 250000}) = 0 (Timeout)
- select(5, [3 4], [], [], {0, 250000}) = 0 (Timeout)
- select(5, [3 4], [], [], {0, 250000}) = 0 (Timeout)
- select(5, [3 4], [], [], {0, 250000}) = 0 (Timeout)
|
2.4 统计各系统调用耗费的时间:
- xxx@xxx:~$ sudo strace -p 9356 -c
- Process 9356 attached
- [ Process PID=9356 runs in 32 bit mode. ]
- System call usage summary for 32 bit mode:
- % time seconds usecs/call calls errors syscall
- ------ ----------- ----------- --------- --------- ----------------
- 98.85 0.030703 24 1275 select
- 1.01 0.000314 0 8498 gettimeofday
- 0.14 0.000042 0 1965 489 read
- 0.00 0.000000 0 27 write
- 0.00 0.000000 0 24 20 open
- 0.00 0.000000 0 4 close
- 0.00 0.000000 0 13 lseek
- 0.00 0.000000 0 2 munmap
- 0.00 0.000000 0 751 nanosleep
- 0.00 0.000000 0 8 2 stat64
- 0.00 0.000000 0 3 fstat64
- 0.00 0.000000 0 17 futex
- ------ ----------- ----------- --------- --------- ----------------
- 100.00 0.031059 12587 511 total
|
2.5 ltrace统计信息
- xxx@xxxt:~$ ltrace -p 12394
- Cannot attach to pid 12394: Operation not permitted
- Could not attach to process. If your uid matches the uid of the target
- process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
- again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
- lubaoquan@zx8200-Lenovo-Product:~$ sudo ltrace -p 12394
- __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __fdelt_chk(3, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __divdi3(250, 0, 1000, 0) = 0
- __moddi3(250, 0, 1000, 0) = 250
- select(5, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __fdelt_chk(3, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __divdi3(250, 0, 1000, 0) = 0
- __moddi3(250, 0, 1000, 0) = 250
- select(5, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
- __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4) = 0
|
2.6 pidstat的详细解释
-r Report page faults andmemory utilization.
When reporting statistics for individualtasks, the following values may be displayed:
UID
The real user identification number of the task being monitored.
USER
The name of the real user owning the task being monitored.
PID
The identification number of the task being monitored.
minflt/s
Total number of minor faults the task has made per second, those whichhave not required loading a memory page from disk.
majflt/s
Total number of major faults the task has made per second, those whichhave required loading a memory page from disk.
VSZ
Virtual Size: The virtual memory usage of entire task in kilobytes.
RSS
Resident Set Size: The non-swapped physical memory used by the task inkilobytes.
%MEM
The tasks's currently used share of available physical memory.
Command
The command name of the task.
- lubaoquan@xxx:~$ pidstat -r -p 12691 1
- Linux 4.4.0-46-generic (zx8200-Lenovo-Product) 11/11/2016 _x86_64_ (4 CPU)
- 09:55:51 AM UID PID minflt/s majflt/s VSZ RSS %MEM Command
- 09:55:52 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:53 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:54 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:55 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:56 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:57 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:58 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:55:59 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
- 09:56:00 AM 1003 12691 0.00 0.00 17060 7216 0.12 runMidlet
|
显示stack使用情况:
-s Report stack utilization.
- Linux 4.4.0-46-generic (zx8200-Lenovo-Product) 11/11/2016 _x86_64_ (4 CPU)
- 10:15:25 AM UID PID StkSize StkRef Command
- 10:15:26 AM 1003 12691 136 20 runMidlet
- 10:15:27 AM 1003 12691 136 20 runMidlet
- 10:15:28 AM 1003 12691 136 20 runMidlet
|
显示IO使用情况:
-d Report I/O statistics (kernels 2.6.20 and later only).
- root@xxx:/proc/12691# pidstat -d -p 12691 1
- Linux 4.4.0-46-generic (zx8200-Lenovo-Product) 11/11/2016 _x86_64_ (4 CPU)
- 10:15:50 AM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
- 10:15:51 AM 1003 12691 0.00 0.00 0.00 runMidlet
- 10:15:52 AM 1003 12691 0.00 0.00 0.00 runMidlet
|
显示CPU使用情况:
-u Report CPU utilization.
背景是运行数独游戏。
- root@xxx:/proc/12691# pidstat -u -p 12691 1
- Linux 4.4.0-46-generic (zx8200-Lenovo-Product) 11/11/2016 _x86_64_ (4 CPU)
- 10:23:30 AM UID PID %usr %system %guest %CPU CPU Command
- 10:23:31 AM 1003 12691 0.00 2.00 0.00 2.00 3 runMidlet
- 10:23:32 AM 1003 12691 1.00 1.00 0.00 2.00 1 runMidlet
- 10:23:33 AM 1003 12691 0.00 2.00 0.00 2.00 0 runMidlet
- 10:23:34 AM 1003 12691 1.00 1.00 0.00 2.00 2 runMidlet
- 10:23:35 AM 1003 12691 1.00 2.00 0.00 3.00 3 runMidlet
|
2.7 ps显示内存
- root@xxxt:/proc/12691# ps -p 12691 -u
- USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
- lubaoqu+ 12691 0.0 0.1 17060 8180 pts/3 Sl 09:50 0:00 ./runMidlet internal com.sun.midp.appmanager.MVMManager
|
使用ps更细致的显示内存占用情况:
ps -p 2395 -eo pid,comm,args,class,vsz,rss,sz | grep Midlet
vsz - virtual memory size of the process in KiB. Device mappings are currently excluded; this is subject to change.
rsz - resident set size, the non-swapped physical memory that a task has used(in killobytes).
sz - size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings are currently excluded; this is subject to change.
- xxx@xxx:~/java/phoneme/temp/bin/i386$ ps -eo pid,comm,args,class,vsz,rss,sz | grep Midlet
- 8952 runMidlet ./runMidlet internal com.su TS 17060 7360 4265
|
2.8 procmem
- xxx@xxx:~/java/phoneme/temp/bin/i386$ sudo procmem -p 8952
- Vss Rss Pss Uss ShCl ShDi PrCl PrDi Name
- ------- ------- ------- ------- ------- ------- ------- -------
- 1748K 1600K 1600K 1600K 0K 0K 1600K 0K /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet
- 12552K 1540K 1540K 1540K 0K 0K 3584K 0K
- 1696K 1200K 618K 36K 1164K 0K 36K 0K /lib/i386-linux-gnu/libc-2.19.so
- 152K 144K 144K 144K 0K 0K 144K 0K
- 132K 100K 100K 100K 0K 0K 100K 0K [heap]
- 152K 152K 76K 0K 0K 152K 0K 0K /SYSV6201000e
- 128K 124K 66K 8K 116K 0K 8K 0K /lib/i386-linux-gnu/ld-2.19.so
- 56K 56K 56K 56K 0K 0K 56K 0K
- 112K 96K 48K 0K 96K 0K 0K 0K /lib/i386-linux-gnu/libgcc_s.so.1
- 96K 88K 44K 0K 88K 0K 0K 0K /lib/i386-linux-gnu/libpthread-2.19.so
- 132K 20K 20K 20K 0K 0K 20K 0K [stack]
- 16K 16K 16K 16K 0K 0K 16K 0K
- 8K 8K 8K 8K 0K 0K 8K 0K /lib/i386-linux-gnu/libc-2.19.so
- 12K 8K 8K 8K 0K 0K 8K 0K
- 8K 8K 8K 8K 0K 0K 8K 0K /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet
- 4K 4K 4K 4K 0K 0K 4K 0K /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/libc-2.19.so
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/libgcc_s.so.1
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/libpthread-2.19.so
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/ld-2.19.so
- 4K 4K 4K 4K 0K 0K 4K 0K
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/libpthread-2.19.so
- 4K 4K 4K 4K 0K 0K 4K 0K /lib/i386-linux-gnu/ld-2.19.so
- 8K 4K 2K 0K 4K 0K 0K 0K [vdso]
- 4K 0K 0K 0K 0K 0K 0K 0K /lib/i386-linux-gnu/libc-2.19.so
- 4K 0K 0K 0K 0K 0K 0K 0K
- 8K 0K 0K 0K 0K 0K 0K 0K [vvar]
- ------- ------- ------- ------- ------- ------- ------- -------
- 17056K 5196K 4386K 3576K 1468K 152K 5620K 0K TOTAL
|
解释如下:
- VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
- RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
- PSS- Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
- USS- Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
VSS (reported as VSZ from ps) is the total accessible address space of a process.This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.
RSS is the total memory actually held in RAM for a process.RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.
PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.
USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaksin a process.
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS