swap内存查看

【转载】https://blog.csdn.net/carefree2005/article/details/124726273

编写脚本找出swap占用top20进程

#!/bin/bash
#script name: swap_check.sh
#author: wuhs
#version: v1
#description: 这是一个检查哪些进程使用了SWAP分区的脚本
do_swap () {
  SUM=0
  OVERALL=0
  #获取进程目录
  for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
        #获取进程PID
    PID=`echo $DIR | cut -d / -f 3`
    #获取进程名称
    PROGNAME=`ps -p $PID -o comm --no-headers`
    #获取进程的所有分区中SWAP值
    for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
    do
          #进程swap值求和
      let SUM=$SUM+$SWAP
    done
    echo "PID=$PID - Swap used: $SUM - $PROGNAME"
    #总共swap分区值求和
    let OVERALL=$OVERALL+$SUM
    SUM=0 
  done
  echo "Overall swap used: $OVERALL"
 }
do_swap > tmp.txt
#查询结果排序并截取top10
cat tmp.txt |awk -F[\ \(] '{print $5,$1,$7}' | sort -n | tail -20
cat tmp.txt |tail -1
rm -rf tmp.txt

 

 

三、swap释放方法

1、查看swap使用情况

(base) [root@test tools]# free -h
total used free shared buff/cache available
Mem: 7.6G 360M 288M 148M 6.9G 6.7G
Swap: 1.0G 635M 388M

2、查看swap挂载

(base) [root@test tools]# swapon -s
文件名 类型 大小 已用 权限
/dev/sda2 partition 1048572 650292 -2

3、取消swap挂载

#取消swap挂载的时候会将swap内网逐步复制到内存中,swap占用越大需要的时间越长
(base) [root@test tools]# swapoff /dev/sda2

4、再次挂载swap
(base) [root@test tools]# swapon /dev/sda2

5、查看swap使用情况
(base) [root@test tools]# free -h
total used free shared buff/cache available
Mem: 7.6G 805M 139M 297M 6.6G 6.2G
Swap: 1.0G 0B 1.0G
————————————————
版权声明:本文为CSDN博主「恒悦sunsite」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/carefree2005/article/details/124726273

 

posted @ 2022-09-26 18:19  He_LiangLiang  阅读(239)  评论(0编辑  收藏  举报