加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

都说Linux是吃内存大户,可你知道具体是哪些进程吃掉了吗?

发布时间:2019-10-26 04:49:52 所属栏目:Windows 来源:Mike
导读:副标题#e# 一个经常被问到的 Linux 问题:为啥 Linux 系统没运行多少程序,显示的可用内存这么少? 其实 Linux 与 Windows 的内存管理不同,会尽量缓存内存以提高读写性能,通常叫做 Cache Memory。 比较老的资料都会介绍 Linux 的 Cache 占用很多并没有关

这个办法在大部分情况下都可以找到占用 Cache 较多的程序和进程。脚本内容如下:

  1. #!/bin/bash  
  2. #Author: Shanker  
  3. #Time: 2016/06/08  
  4. #set -e  
  5. #set -u  
  6. #you have to install linux-fincore  
  7. if [ ! -f /usr/local/bin/linux-fincore ]  
  8. then  
  9.     echo "You haven't installed linux-fincore yet"  
  10.     exit  
  11. fi  
  12. #find the top 10 processs' cache file  
  13. ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids   
  14. #find all the processs' cache file  
  15. #ps -e -o pid>/tmp/cache.pids  
  16. if [ -f /tmp/cache.files ]  
  17. then  
  18.     echo "the cache.files is exist, removing now "  
  19.     rm -f /tmp/cache.files  
  20. fi  
  21. while read line  
  22. do  
  23.     lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files   
  24. done</tmp/cache.pids  
  25. if [ -f /tmp/cache.fincore ]  
  26. then  
  27.     echo "the cache.fincore is exist, removing now"  
  28.     rm -f /tmp/cache.fincore  
  29. fi  
  30. for i in `cat /tmp/cache.files`  
  31. do  
  32.     if [ -f $i ]  
  33.     then  
  34.         echo $i >>/tmp/cache.fincore  
  35.     fi  
  36. done  
  37. linux-fincore -s  `cat /tmp/cache.fincore`  
  38. rm -f /tmp/cache.{pids,files,fincore} 

比较遗憾的是,linux-ftools 目前已经不再维护了。在新版本的操作系统上没法编译好这个程序,所以这个方法失效了。

再次通过万能的 Google 搜索,后来我找到了 pcstat 这个工具,pcstat 使用 Go 语言开发,功能基本和 linux-ftools 一样 。

项目地址:https://github.com/tobert/pcstat

然后我修改了 Shanker 的脚本,让它使用 pcstat 来进行处理,这样就可以很好的找到 Cache 所占用的情况。修改后的脚本如下:

  1. #!/bin/bash  
  2. #you have to install pcstat  
  3. if [ ! -f /data0/brokerproxy/pcstat ]  
  4. then  
  5.     echo "You haven't installed pcstat yet"  
  6.     echo "run "go get github.com/tobert/pcstat" to install"  
  7.     exit  
  8. fi  
  9. #find the top 10 processs' cache file  
  10. ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids  
  11. #find all the processs' cache file  
  12. #ps -e -o pid>/tmp/cache.pids  
  13. if [ -f /tmp/cache.files ]  
  14. then  
  15.     echo "the cache.files is exist, removing now "  
  16.     rm -f /tmp/cache.files  
  17. fi  
  18. while read line  
  19. do  
  20.     lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files   
  21. done</tmp/cache.pids  
  22. if [ -f /tmp/cache.pcstat ]  
  23. then  
  24.     echo "the cache.pcstat is exist, removing now"  
  25.     rm -f /tmp/cache.pcstat 
  26. fi  
  27. for i in `cat /tmp/cache.files`  
  28. do  
  29.     if [ -f $i ]  
  30.     then  
  31.         echo $i >>/tmp/cache.pcstat  
  32.     fi  
  33. done  
  34. /data0/brokerproxy/pcstat  `cat /tmp/cache.pcstat`  
  35. rm -f /tmp/cache.{pids,files,pcstat} 

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读