SHELL:Find Memory Usage In Linux (统计每个程序内存使用情况)
转载一个shell统计linux系统中每个程序的内存使用情况,因为内存结构非常复杂,不一定100%精确,此shell可以在Ghub上下载。
[root@db231 ~]# ./memstat.sh
Private + Shared = RAM used Program
540.78 mb + 175.56 mb = 716.35 mb oracle(25)
24.50 mb + 327.00 kb = 24.82 mb iscsiuio
15.37 mb + 341.00 kb = 15.70 mb yum-updatesd
12.51 mb + 853.00 kb = 13.35 mb gdmgreeter
11.69 mb + 74.00 kb = 11.77 mb tnslsnr
4.74 mb + 302.00 kb = 5.03 mb Xorg
1.42 mb + 1016.00 kb = 2.41 mb smbd(2)
2.04 mb + 347.00 kb = 2.38 mb gdm-rh-security
1.83 mb + 433.00 kb = 2.25 mb sshd(2)
1.51 mb + 379.00 kb = 1.88 mb iscsid(2)
748.00 kb + 958.00 kb = 1.66 mb gdm-binary(2)
1.02 mb + 134.00 kb = 1.15 mb cupsd
...
--------------------------------------------------------
808.08 mb
========================================================
[root@db231 ~]# free
total used free shared buffers cached
Mem: 8174740 7339076 835664 0 486208 6085484
-/+ buffers/cache: 767384 7407356
Swap: 8393920 19996 8373924
memory stat shell
#!/bin/bash
# Make sure only root can run our script if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi ### Functions
#This function will count memory statistic for passed PID
get_process_mem ()
{
PID=$1
#we need to check if 2 files exist
if [ -f /proc/$PID/status ];
then
if [ -f /proc/$PID/smaps ];
then
#here we count memory usage, Pss, Private and Shared = Pss-Private
Pss=`cat /proc/$PID/smaps | grep -e "^Pss:" | awk '{print $2}'| paste -sd+ | bc `
Private=`cat /proc/$PID/smaps | grep -e "^Private" | awk '{print $2}'| paste -sd+ | bc `
#we need to be sure that we count Pss and Private memory, to avoid errors
if [ x"$Rss" != "x" -o x"$Private" != "x" ];
then let Shared=${Pss}-${Private}
Name=`cat /proc/$PID/status | grep -e "^Name:" |cut -d':' -f2`
#we keep all results in bytes
let Shared=${Shared}*1024
let Private=${Private}*1024
let Sum=${Shared}+${Private} echo -e "$Private + $Shared = $Sum \t $Name"
fi
fi
fi
} #this function make conversion from bytes to Kb or Mb or Gb
convert()
{
value=$1
power=0
#if value 0, we make it like 0.00
if [ "$value" = "0" ];
then
value="0.00"
fi #We make conversion till value bigger than 1024, and if yes we divide by 1024
while [ $(echo "${value} > 1024"|bc) -eq 1 ]
do
value=$(echo "scale=2;${value}/1024"|bc)
let power=$power+1
done #this part get b,kb,mb or gb according to number of divisions
case $power in
0) reg=b;;
1) reg=kb;;
2) reg=mb;;
3) reg=gb;;
esac echo -n "${value} ${reg} "
} #to ensure that temp files not exist
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3 #if argument passed script will show statistic only for that pid, of not we list all processes in /proc/ #and get statistic for all of them, all result we store in file /tmp/res
if [ $# -eq 0 ]
then
pids=`ls /proc | grep -e [0-9] | grep -v [A-Za-z] `
for i in $pids
do
get_process_mem $i >> /tmp/res
done
else
get_process_mem $1>> /tmp/res
fi #This will sort result by memory usage
cat /tmp/res | sort -gr -k 5 > /tmp/res2 #this part will get uniq names from process list, and we will add all lines with same process list
#we will count nomber of processes with same name, so if more that 1 process where will be
# process(2) in output
for Name in `cat /tmp/res2 | awk '{print $6}' | sort | uniq`
do
count=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $6}}'|wc -l| awk '{print $1}'`
if [ $count = "1" ];
then
count=""
else
count="(${count})"
fi VmSizeKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $1}}' | paste -sd+ | bc`
VmRssKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $3}}' | paste -sd+ | bc`
total=`cat /tmp/res2 | awk '{print $5}' | paste -sd+ | bc`
Sum=`echo "${VmRssKB}+${VmSizeKB}"|bc`
#all result stored in /tmp/res3 file
echo -e "$VmSizeKB + $VmRssKB = $Sum \t ${Name}${count}" >>/tmp/res3
done #this make sort once more.
cat /tmp/res3 | sort -gr -k 5 | uniq > /tmp/res #now we print result , first header
echo -e "Private \t + \t Shared \t = \t RAM used \t Program"
#after we read line by line of temp file
while read line
do
echo $line | while read a b c d e f
do
#we print all processes if Ram used if not 0
if [ $e != "0" ]; then
#here we use function that make conversion
echo -en "`convert $a` \t $b \t `convert $c` \t $d \t `convert $e` \t $f"
echo ""
fi
done
done < /tmp/res #this part print footer, with counted Ram usage
echo "--------------------------------------------------------"
echo -e "\t\t\t\t\t\t `convert $total`"
echo "========================================================" # we clean temporary file
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3
--注意全角引号
[oracle@dbserver89 ~]$ pmap -d 5502
5502: oracleora11g (LOCAL=NO)
Address Kbytes Mode Offset Device Mapping
0000000000400000 183540 r-x-- 0000000000000000 008:00006 oracle
000000000b93c000 1884 rwx-- 000000000b33c000 008:00006 oracle
000000000bb13000 304 rwx-- 000000000bb13000 000:00000 [ anon ]
000000001819f000 544 rwx-- 000000001819f000 000:00000 [ anon ]
0000000060000000 32768 rwxs- 0000000000000000 000:0000c 13 (deleted)
ffffffffff600000 8192 ----- 0000000000000000 000:00000 [ anon ]
...
mapped: 4753296K writeable/private: 8536K shared: 4507648K
SHELL:Find Memory Usage In Linux (统计每个程序内存使用情况)的更多相关文章
- 5 commands to check memory usage on Linux
Memory Usage On linux, there are commands for almost everything, because the gui might not be always ...
- Linux查看CPU和内存使用情况总结
Linux查看CPU和内存使用情况:http://www.cnblogs.com/xd502djj/archive/2011/03/01/1968041.html 在做Linux系统优化的时候,物理内 ...
- Linux查看CPU和内存使用情况 【转】
Linux查看CPU和内存使用情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 ...
- linux 下查看系统内存使用情况的方法
在Windows系统中查看内存的使用情况很简单,想必大家都已经耳熟能详了,那么在linux系统如何查看内存使用情况呢?下面和大家分享在Linux 下查看内存使用情况的free命令: [root@scs ...
- linux free 命令 查看内存使用情况
查看Linux服务器下的内存使用情况,可以使用命令free -m [root@localhost ~]$ free // 以KB为单位显示内存使用情况 [root@localhost ~]$ free ...
- top命令 Linux查看CPU和内存使用情况
一.top命令 top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要.但是,它的缺点是会消耗很多系统资源. 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分 ...
- top命令 Linux查看CPU和内存使用情况,cpu监控之一
一.top命令 top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要.但是,它的缺点是会消耗很多系统资源. 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分 ...
- Linux查看CPU和内存使用情况(转)
在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...
- Linux查看CPU和内存使用情况
在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...
随机推荐
- (转载)AS3领航系列教程 之 AS3程序的入口
(转载)http://blog.csdn.net/wibrst/article/details/1861828 要实践本教程, 您需要安装以下软件: Flash CS3 AS3程序的入口 众所周 ...
- struts2错误:The Struts dispatcher cannot be found.
struts2错误:The Struts dispatcher cannot be found. The Struts dispatcher cannot be found. This is usua ...
- CSS浏览器兼容性----Hack
CSS Hack大致有3种表现形式,CSS类内部Hack.选择器Hack以及HTML头部引用(if IE)Hack,CSS Hack主要针对IE浏览器.类内部Hack:比如 IE6能识别下划线&quo ...
- ListControl常用操作汇总
本文根据本人在项目中的应用,来谈谈CListCtrl的部分用法及技巧.当初学习时,查了很多资料,零零碎碎的作了些记录,现在主要是来做个总结,方便以后查阅.主要包括以下十三点内容:基本操作.获取选中行的 ...
- JavaScript高级程序设计51.pdf
(续上篇) 模拟鼠标事件 var btn=document.getElementById("myBtn"); //创建事件对象 var event=document.createE ...
- Spark RDD/Core 编程 API入门系列之map、filter、textFile、cache、对Job输出结果进行升和降序、union、groupByKey、join、reduce、lookup(一)
1.以本地模式实战map和filter 2.以集群模式实战textFile和cache 3.对Job输出结果进行升和降序 4.union 5.groupByKey 6.join 7.reduce 8. ...
- Yii之权限管理扩展 srbac
最近在研究 Yii 的权限控制功能,尽管Yii 自身提供了一个简单的权限管理,但是很多时候,我们还是需要对其做一点扩展. 在这里,我向大家推荐一个不错的扩展:SRBAC. 在Yii的官方网站的exte ...
- 在线App开发平台——应用之星傻瓜式开发平台
随着智能手机及APP应用程序的普及,越来越多的企业和个人意识到APP的营销价值,出于对技术的敬畏,很多企业下意识认为开发APP是一个有难度的技术活,所以很多时候有心无力,也担心APP的后续的技术支持. ...
- UF2.0、O4、UFT、TA众明星背后的秘密
UF2.0--经纪业务运营平台 O4--投资交易管理系统软件 UFT--证券极速交易系统软件 TA--登记过户系统 -- 说到恒生在业内的明星产品,太多了,小编一口气说不完,但小编只知其一,殊不知这些 ...
- https_request请求接口返回数据
定义一个https_request方法 <?php function https_request($url, $data = null) { $curl = curl_init(); curl_ ...