Memory RedHat内存使用率高

Memory RedHat内存使用率高,memory,virtual-machine,redhat,Memory,Virtual Machine,Redhat,如果你愿意的话,我正在寻求帮助 我在RedHat 6.5上有一个32gb内存的虚拟机。 免费显示已使用24.6gb,免费显示8.2gb。只有418mb缓存,1.8gb缓冲区 执行了一个top并按使用的虚拟机排序,我只能解释使用的24.6gb中的6gb。 “ps aux”不会显示任何可能占用内存的进程 我很困惑,想找些建议,看看哪里能找到记忆的来源 任何帮助都将不胜感激。下面的Bash脚本将帮助您确定哪个应用程序正在消耗多少内存 #!/bin/bash # Make sure only root

如果你愿意的话,我正在寻求帮助

我在RedHat 6.5上有一个32gb内存的虚拟机。 免费显示已使用24.6gb,免费显示8.2gb。只有418mb缓存,1.8gb缓冲区

执行了一个top并按使用的虚拟机排序,我只能解释使用的24.6gb中的6gb。 “ps aux”不会显示任何可能占用内存的进程

我很困惑,想找些建议,看看哪里能找到记忆的来源


任何帮助都将不胜感激。

下面的Bash脚本将帮助您确定哪个应用程序正在消耗多少内存

#!/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
#/bin/bash
#确保只有root用户才能运行我们的脚本
如果[“$(id-u)”!=“0”];然后
echo“此脚本必须以root用户身份运行”1>&2
出口1
fi
###功能
#此函数将统计通过的PID的内存统计信息
获取进程成员()
{
PID=$1
#我们需要检查是否存在2个文件
如果[-f/proc/$PID/状态];
然后
如果[-f/proc/$PID/smaps];
然后
#这里我们计算内存使用量,Pss,Private和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`
#我们需要确保计算Pss和私有内存,以避免错误
如果[x“$Rss”!=“x”-o x“$Private”!=“x”];
然后
let Shared=${Pss}-${Private}
Name=`cat/proc/$PID/status | grep-e“^Name:| cut-d':'-f2`
#我们以字节为单位保存所有结果
let Shared=${Shared}*1024
让Private=${Private}*1024
设Sum=${Shared}+${Private}
echo-e“$Private+$Shared=$Sum\t$Name”
fi
fi
fi
}
#此函数用于将字节转换为Kb、Mb或Gb
转换()
{
价值=$1
功率=0
#如果值为0,我们将其设为0.00
如果[“$value”=“0”];
然后
value=“0.00”
fi
#我们进行转换,直到值大于1024,如果是,我们除以1024
而[$(echo“${value}>1024”| bc)-eq 1]
做
值=$(echo“scale=2;${value}/1024”| bc)
设功率=$power+1
完成
#这部分根据分区的数量获得b、kb、mb或gb
案例$power in
0)reg=b;;
1) reg=kb;;
2) reg=mb;;
3) reg=gb;;
以撒
echo-n“${value}${reg}”
}
#确保临时文件不存在
[[-f/tmp/res]]和&rm-f/tmp/res
[[-f/tmp/res2]]&&rm-f/tmp/res2
[[-f/tmp/res3]]&&rm-f/tmp/res3
#若参数传递脚本将只显示该pid的统计信息,当然不会–我们在/proc/#中列出所有进程,并获取所有进程的统计信息,所有结果存储在文件/tmp/res中
如果[$#-eq 0]
然后
pids=`ls/proc | grep-e[0-9]| grep-v[A-Za-z]`
对于我来说,我是$pids
做
获取\u进程\u mem$i>>/tmp/res
完成
其他的
获取进程内存$1>>/tmp/res
fi
#这将根据内存使用情况对结果进行排序
cat/tmp/res | sort-gr-k 5>/tmp/res2
#这部分将从进程列表中获取uniq名称,我们将添加具有相同进程列表的所有行
#我们将计算具有相同名称的进程的名称,所以如果超过1个进程,将在哪里
#输出中的进程(2)
对于'cat/tmp/res2 | awk'{print$6}'中的名称| sort | uniq`
做
count=`cat/tmp/res2 | awk-v src=$Name'{if($6==src){print$6}}}}| wc-l | awk'{print$1}'`
如果[$count=“1”];
然后
count=“”
其他的
count=“(${count})”
fi
VmSizeKB=`cat/tmp/res2 | awk-v src=$Name'{if($6==src){print$1}}}'|粘贴-sd+| bc`
VmRssKB=`cat/tmp/res2 | awk-v src=$Name'{if($6==src){print$3}}}'|粘贴-sd+| bc`
总计=`cat/tmp/res2 | awk'{print$5}'|粘贴-sd+| bc`
Sum=`echo“${VmRssKB}+${VmSizeKB}”| bc`
#所有结果都存储在/tmp/res3文件中
echo-e“$VmSizeKB+$VmRssKB=$Sum\t${Name}${count}”>>/tmp/res3
完成
#这又一次使我感到失望。
cat/tmp/res3 | sort-gr-k 5 | uniq>/tmp/res
#现在我们打印结果,第一个标题
echo-e“专用\t+\t共享\t=\t使用的RAM\t程序”
#在我们逐行读取临时文件之后
读行时
做
回显$line |同时读取b c d e f
做
#如果使用Ram,则打印所有进程,如果不是0
如果[$e!=“0”];然后
#在这里,我们使用进行转换的函数
echo-en“`convert$a`\t$b\t`convert$c`\t$d\t`convert$e`\t$f”
回声“”
fi
完成
已完成
我要大胆尝试一下。如果无法访问机器或获取其他信息,则很难进行故障排除


/tmp文件系统的特殊之处在于它完全存在于内存中。还有一些类似的例子,但是/tmp是一种特殊的花。检查此目录上的磁盘使用情况,您可能会看到内存消耗的位置。(du-sh/tmp)

正如我所说,只有418mb缓存,1.8gb缓冲区。因此,32gb的总ram,据报告使用了24.6。缓冲区/缓存中的容量刚刚超过2.2gb,请检查“ps aux | sort-nk+4 | tail”的输出