检查每个进程和子进程的内存 [英] Check memory per processes and subprocesses

查看:104
本文介绍了检查每个进程和子进程的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个脚本,以显示mysqld的每个进程和子进程使用了​​多少.您可以在代码中看到我所做的事情.

#!/bin/bash
#file contains the output of: pstree mysql -a -p |awk '{print $1;}' | sed 's/|-        {mysqld},//' >> psadd
filename='psadd'
#total= '0'
echo Start
while read p; do
    memU= cat /proc/$p/smaps |grep -e Private -e Shared |awk '{print $2}' |awk '{total = total + $1}END{print total}'
    echo "Process ID:"$p  "Memory Usage:"$memU
total="$((total+memu))"
echo "This is the current running total:" $total
done < $filename
echo "Total=" $total

如果您有任何想法,请多多关照.

解决方案

计算进程的内存使用量是... 复杂.我通常使用proc的RSS(常驻集大小),即进程在内存中保留的内存量,其他proc不共享.

以下内容查找MySQL守护程序的进程ID,并使用ps输出不带标题的RSS值.最后,将其乘以四即可得到KiB中的RSS大小. (默认页面大小为4 KiB.)

ps 包含大量信息-玩得开心!

shell

ps -o rss= -p `pidof mysqld` | awk '{print $1*4, "KiB"}'

输出

7808 KiB

I am attempted to create a script that show how much each process and subprocess of mysqld is using. You can see what I have done in my code.

#!/bin/bash
#file contains the output of: pstree mysql -a -p |awk '{print $1;}' | sed 's/|-        {mysqld},//' >> psadd
filename='psadd'
#total= '0'
echo Start
while read p; do
    memU= cat /proc/$p/smaps |grep -e Private -e Shared |awk '{print $2}' |awk '{total = total + $1}END{print total}'
    echo "Process ID:"$p  "Memory Usage:"$memU
total="$((total+memu))"
echo "This is the current running total:" $total
done < $filename
echo "Total=" $total

Please if you have any ideas they will be much appreciated.

解决方案

Calculating a processes memory usage is... complicated. I generally use a proc's RSS -- Resident Set Size -- the amount of memory a process is holding in memory, that isn't shared by other procs.

The following finds the process ID of the MySQL daemon, and uses ps to output the RSS value with no header. Lastly it multiplies this by four to get the RSS size in KiB. (Default pagesize is 4 KiB.)

ps has tons of information -- have fun!

shell

ps -o rss= -p `pidof mysqld` | awk '{print $1*4, "KiB"}'

output

7808 KiB

这篇关于检查每个进程和子进程的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆