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

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

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

因为 Kernel 的 Page Cache Table 只存储 Data Block 的引用而不是文件名,即文件的 Inode 信息。所以并没有任何一个工具运行一次就可以找出所有的文件使用缓存的情况。所以使用 linux-fincore 这个工具也只能加文件名来判断该文件是否被缓存,如果缓存,大小是多少。问题是你不能随便猜哪个文件是否被缓存吧。

Shanker 提供了一个脚本来解决此问题,那就是查看哪些进程使用的物理内存最多,就找到该进程打开的文件,然后用 fincore 来查看这些文件的缓存使用率。

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

#!/bin/bash  

#Author: Shanker  

#Time: 2016/06/08  

#set -e  

#set -u  

#you have to install linux-fincore  

if [ ! -f /usr/local/bin/linux-fincore ]  

then  

    echo "You haven't installed linux-fincore yet"  

    exit  

fi  

#find the top 10 processs' cache file  

ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids   

#find all the processs' cache file  

#ps -e -o pid>/tmp/cache.pids  

if [ -f /tmp/cache.files ]  

then  

    echo "the cache.files is exist, removing now "  

    rm -f /tmp/cache.files  

fi  

while read line  

do  

    lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files   

done</tmp/cache.pids  

if [ -f /tmp/cache.fincore ]  

then  

    echo "the cache.fincore is exist, removing now"  

    rm -f /tmp/cache.fincore  

fi  

for i in `cat /tmp/cache.files`  

do  

    if [ -f $i ]  

    then  

        echo $i >>/tmp/cache.fincore  

    fi  

done  

linux-fincore -s  `cat /tmp/cache.fincore`  

rm -f /tmp/cache.{pids,files,fincore} 

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

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

(编辑:大连站长网)

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