SHELL:Find Memory Usage In Linux (统计每个程序内存使用情况)

news/2024/7/8 2:02:36 标签: shell, awk, 数据库

转载一个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

转载于:https://www.cnblogs.com/travel6868/p/5016164.html


http://www.niftyadmin.cn/n/1120441.html

相关文章

物联网未来趋势:边缘计算正渐渐兴起

物联网这张有史以来最大的“网”正在悄然地改变着我们的生活方式。我们更加喜欢将照片存入云端&#xff0c;而不是简单地放在手机内存&#xff1b;更喜欢在家连上WiFi&#xff0c;在户外更愿意接入4G网络&#xff1b;相比于繁琐的购买信息的输入&#xff0c;更愿意一键网购....…

SQL中的变量

变量分为两种&#xff1a;系统变量和自定义变量 系统变量 系统定义好的变量&#xff1a;大部分的时候用户根本不需要使用系统变量&#xff1b;系统变量是用来控制服务器表现的&#xff1a;如autocommit、auto_increment、increment等 查看系统变量 show variables&#xff1b; …

SpringMVC学习笔记1(整合Mybatis参数绑定)

前言 SpringMVC 第一天学习大纲&#xff1a; SpringMVC 介绍入门程序SpringMVC 架构讲解 框架结构组件说明SpringMVC 整合 Mybatis参数绑定 SpringMVC 默认支持的的类型简单数据类型POJO 类型POJO 包装类型自定义参数绑定SpringMVC 和 Struts2 的区别一、SpringMVC介绍 1、Spri…

多种替身邮方法总结!

1&#xff0c;gmail的替身方法&#xff1a; 原理&#xff1a; 1、Gmail注册时允许特殊字符 . 和 ,但收信时&#xff0c;将这两个字符视为无效字符 2、用户名不区分大小写 因此&#xff0c;利用这些特点&#xff0c;可以建立替身邮箱&#xff0c;如注册一个ID为hjdhdgysdghdshg…

MYSQL类型与JAVA类型对应表

类型名称显示长度数据库类型JAVA类型JDBC类型索引(int) VARCHARLNVARCHARjava.lang.String12CHARNCHARjava.lang.String1BLOBLNBLOBjava.lang.byte[]-4TEXT65535VARCHARjava.lang.String-1 INTEGER4INTEGER UNSIGNEDjava.lang.Long4TINYINT3TINYINT UNSIGNEDjava.lang.…

Jenkins+git+tomcat 自动化持续部署

新建项目添加jenkins项目名称 tomcat01选择 构建一个自由风格的软件项目点击 OK源码管理 填写源代码的路径这里是用git&#xff0c;所有选择git选项https://github.com/bingozhou/tomcat.git4. 构建触发器选择 Poll SCM日程表 填 * * * * * (表示每分钟检测一次git仓…

finally解析

为什么80%的码农都做不了架构师&#xff1f;>>> 问题&#xff1a; 1.什么时候使用finally语句块&#xff1f;&#xff1f;&#xff1f; 2.finally语句块在try或者catch语句中return返回之前还是之后执行&#xff1f;&#xff1f;&#xff1f; 3.什么情况下finally语…

OCP-1Z0-051 第159题 insert语句中使用子查询

一、原题 View the Exhibit and examine the structure of the CUSTOMERS table. NEW_CUSTOMERS is a new table with the columns CUST_ID, CUST_NAME and CUST_CITY that have the same data types and size as the corresponding columns in the CUSTOMERS table. Evaluat…