Linux查看进程内存占用情况以MB为单位进行直观显示【测试成功】

艺帆风顺 发布于 2025-04-03 21 次阅读


一、需求背景

    Linux默认top或者ps命令,查看出来占用情况显示单位不友好、不易懂。

二、命令参数

   1、 以MB为单位显示

    ps aux --sort=-%mem | awk '{print $6/1024 " MB", $11}' | head -n 11

   2、 以MB为单位显示,同时显示进程ID

    ps aux --sort=-%mem | awk '{print $2, $6/1024 " MB", $11}' | head -n 11

    [root@Devlops ~]# ps aux --sort=-%mem | awk '{print $2, $6/1024 " MB", $11}' | head -n 11PID 0 MB COMMAND2390 301.926 MB java2887 120.707 MB /opt/py3/bin/python3.62842 119 MB /opt/py3/bin/python3.62873 117.539 MB /opt/py3/bin/python3.62118 117.258 MB mysqld2843 115.965 MB /opt/py3/bin/python3.62504 44.9688 MB /app/gogs/gogs1221 40.5703 MB /usr/bin/dockerd1022 20.9922 MB /usr/bin/containerd1952 17.1953 MB /bin/drone-server

    3、htop命令

      [root@Devlops ~]# yum -y install htop已加载插件:fastestmirrorRepository base is listed more than once in the configurationRepository updates is listed more than once in the configurationRepository extras is listed more than once in the configurationRepository centosplus is listed more than once in the configurationLoading mirror speeds from cached hostfileepel/x86_64/metalink | 7.0 kB 00:00:00  * base: mirrors.aliyun.com * epel: mirror.lzu.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.combase | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/2): epel/x86_64/updateinfo | 1.0 MB 00:00:01 (2/2): epel/x86_64/primary_db | 7.0 MB 00:00:01 正在解决依赖关系--> 正在检查事务---> 软件包 htop.x86_64.0.2.2.0-3.el7 将被 安装--> 解决依赖关系完成
      依赖关系解决
      ================================================================================================================ Package 架构 版本 源 大小================================================================================================================正在安装: htop x86_64 2.2.0-3.el7 epel 103 k
      事务概要================================================================================================================安装 1 软件包
      总下载量:103 k安装大小:218 kDownloading packages:htop-2.2.0-3.el7.x86_64.rpm | 103 kB 00:00:00 Running transaction checkRunning transaction testTransaction test succeededRunning transaction 正在安装 : htop-2.2.0-3.el7.x86_64 1/1 验证中 : htop-2.2.0-3.el7.x86_64 1/1
      已安装: htop.x86_64 0:2.2.0-3.el7
      完毕!