站長新書 站長開講 首頁 最新文章 站長著作及審校 FreeBSD 筆記 Linux 筆記 Windows 筆記 虛擬化筆記 網管人雜誌 遊山玩水 關於本站
站長新書 VMware vSphere ICM 團購開跑了!!

Memory-查看目前系統記憶體使用狀態

前言

因為管理的 CentOS 伺服器常常運作一陣子之後記憶體就被吃光了,請教了 Akira 後賜給了此 Script 可以很方便查看到底記憶體是被哪幾支吃記憶體怪獸給吃光的,在得到了 Akira 的同意後把此 Script 給留了下來。請注意:以下 Script 只適合我的環境,若您取用請自行調整,恕不負損壞之責。

實作環境

CentOS 5.1 (Linux 2.6.18-53.1.4.el5)

Script 功能說明

memuse.sh 達成如下需求

Script 內容說明

查尋系統記憶體使用情況的 memuse.sh Script 內容如下

 #!/bin/sh
 #$Id: memuse.sh, v0.1 2008/4/21 Akira Exp $
 #View Memory Use Status
 echo ""
 vmstat -s -S M
 echo ""
 PS=`ls /proc/*[0-9]*|grep :` ;
 PSList=`echo $PS | tr -d '/proc' | tr -d ':' | sort `;
 echo -e "#PID\t\t#MEMSize\t\t#MEMUse\t\t\t#Name\t\t#CMD"
 for PID in $PSList
   do
  test -e /proc/$PID/status && \
  sPID=`cat /proc/$PID/status | grep ^Pid:` && \
  sMEMsize=`cat /proc/$PID/status | grep ^VmSize: | awk '{print $1,$2/1024,"M"}'` && \
  sMEMuse=`cat /proc/$PID/status | grep ^VmRSS: | awk '{print $1,$2/1024,"M"}'` && \
  sName=`cat /proc/$PID/status | grep ^Name:` && \
  if [ -e /proc/$PID/exe ]
  then
     sCMD=`ls -alF /proc/$PID/exe | awk '{print }' | tr -d '$*' | awk '{print $11}'`
  fi
  echo -e "$sPID\t$sMEMsize\t$sMEMuse\t$sName\tCMD:\t$sCMD" | egrep 'VmSize|VmRSS' >> /tmp/mem001
 done
  cat /tmp/mem001 | sort -n -r -t " " -k 2 | head -10
  rm -rf /tmp/mem001

Script 執行結果

 #./memuse.sh
         2026 M total memory
         1974 M used memory
         1002 M active memory
          883 M inactive memory
           52 M free memory
           50 M buffer memory
         1781 M swap cache
         4094 M total swap
            0 M used swap
         4094 M free swap
        32664 non-nice user cpu ticks
           24 nice user cpu ticks
        40985 system cpu ticks
     77586536 idle cpu ticks
       245316 IO-wait cpu ticks
          451 IRQ cpu ticks
         4067 softirq cpu ticks
            0 stolen cpu ticks
       435287 pages paged in
      8556008 pages paged out
            0 pages swapped in
           35 pages swapped out
    202428647 interrupts
     18940956 CPU context switches
   1208576595 boot time
        54664 forks
 #PID            #MEMSize                #MEMUse                 #Name           #CMD
 Pid:    2831    VmSize: 214.617 M       VmRSS: 16.4688 M        Name:   java    CMD:    /usr/local/Dell
 Pid:    3128    VmSize: 69.2812 M       VmRSS: 4.28906 M        Name:   named   CMD:    /usr/sbin/named
 Pid:    2835    VmSize: 38.1016 M       VmRSS: 4.26953 M        Name:   mrmonitord      CMD:    /usr/local/bin/mrmonitord
 Pid:    8663    VmSize: 34.6172 M       VmRSS: 30.8945 M        Name:   squid   CMD:    /usr/sbin/squid
 Pid:    2657    VmSize: 8.87891 M       VmRSS: 1.81641 M        Name:   sendmail        CMD:    /usr/sbin/sendmail.sendmail
 Pid:    8499    VmSize: 8.78906 M       VmRSS: 1.58203 M        Name:   sshd    CMD:    /usr/sbin/sshd
 Pid:    8497    VmSize: 8.78906 M       VmRSS: 2.58203 M        Name:   sshd    CMD:    /usr/sbin/sshd
 Pid:    2934    VmSize: 8.08984 M       VmRSS: 1.29688 M        Name:   mrmonitord      CMD:    /usr/local/bin/mrmonitord
 Pid:    2665    VmSize: 7.83203 M       VmRSS: 1.45703 M        Name:   sendmail        CMD:    /usr/sbin/sendmail.sendmail
 Pid:    2719    VmSize: 7.53516 M       VmRSS: 1.23047 M        Name:   squid   CMD:    /usr/sbin/squid
Go To Oddmuse OrgGo To FreeBSD OrgCreative Commons 2.5 Taiwansitestates.com