./getPIDMemInfo.sh java#!/bin/bash get_main() { #注意 awk print 有些系統是 $2 有些是 $1 target=$(ps -ef | grep -v grep | grep $1 | awk '{print $2}') para1="grep -e State: -e Pid: -e Ppid: -e VmRSS: -e VmData: -e Threads:" para2="grep -v TracerPid:" for pid in $target do i=0 if [ -d /proc/$pid ]; then #echo -n pid=$pid, `awk '{print $3 ", ppid="$4 ", tid_cnt="$20}' /proc/$pid/stat` val=$(cat /proc/$pid/status | $para1 | $para2 | awk '{print $2}') fi for ret in $val do if [ $i -eq 0 ]; then echo -n $ret elif [ $i -eq 1 ]; then echo -n " pid="$ret elif [ $i -eq 2 ]; then echo -n " ppid="$ret elif [ $i -eq 3 ]; then echo -n " VmRss="$(($ret/1024))"MB" vmrss=$ret elif [ $i -eq 4 ]; then echo -n " VmData="$(($ret/1024))"MB" vmdata=$ret elif [ $i -eq 5 ]; then echo -n " threads="$ret fi i=$((i+1)) done echo -n -e '\t' "TotalMem="$(($(($vmrss+$vmdata))/1024))"MB" if [ -d /proc/$pid/fd ]; then echo " fd="$(ls -l /proc/$pid/fd | wc -l) else echo fi done echo ------ } get_memory() { ret=$(free -m | awk 'NR==3 {print "free="$4}') echo $ret"MB" } if [ $# -lt 1 ]; then echo "enter a execution name, ex: python3, java, chrome" exit else echo "execution name:$1" fi while true; do get_memory get_main $1 sleep 3 done
free 比較舊版本的格式
total used free shared buffers cached Mem: 434 258 175 1 6 82 -/+ buffers/cache: 169 264 Swap: 0 0 0
ref:
1 .Linux Memory Usage 記憶體使用詳解
2. /proc/pid/stat說明
3. /proc/pid/status說明
4. 《嵌入式Linux内存使用与性能优化》笔记
沒有留言:
張貼留言