Linux:在Linux中查看进程占用内存大小的方法

在开发和运维过程中,经常会遇到的问题是一个进程到底占用了多大的内存。以下通过常用的2个命令来说明如何获取进程占用的内存

比如有一个java进程,它的pid=7020

$ps -ef | grep LogAgent | grep ^grep
usrapp 7020 1 0 15:23 ? 00:00:02 java -jar LogAgent.jar

1、使用ps

$ps -o vsz,rss,sz,size,pid -q7020
VSZ RSS SZ SIZE PID
4444604 39160 1111151 4268292 7020
以上显示的数字的单位是KB ,其中vsz,rss,sz,size的含义如下:

|------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| name | description |
| vsz | virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject to change. (alias vsize). |
| rss | resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz). |
| sz | size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings are currently excluded; this is subject to change. See vsz and rss. |
| size | approximate amount of swap space that would be required if the process were to dirty all writable pages and then be swapped out. This number is very rough! |

其中最关注的是RSS,它是实际占用的物理内存的大小。

2、使用top

$top -n 1 -p 7020
top - 16:29:32 up 166 days, 5:59, 3 users, load average: 0.25, 0.31, 0.48
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 3.4 sy, 0.0 ni, 96.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 7388532 total, 158576 free, 5822636 used, 1407320 buff/cache
KiB Swap: 16777212 total, 12173780 free, 4603432 used. 1012880 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7020 usrapp 20 0 4444604 39160 12444 S 0.0 0.5 0:02.15 java
从执行结果可以看到:
ps. VSZ=top.VIRT
ps. RSS=top.RES

参考文档

ps(1) - Linux manual page

top(1) - Linux manual page

相关推荐
laolitou_10242 小时前
CentOS 7安装部署RabbitMQ
linux·centos·rabbitmq
aitav04 小时前
⚡ WSL2 搭建 s5p6818 Linux 嵌入式开发平台 (part 3):Wifi驱动移植、ssh移植、e2fsprogs移植
linux·wifi·ssh·嵌入式·e2fsprogs
南枝异客6 小时前
CentOS 7 网络连接问题
linux·运维·centos
牛奶咖啡137 小时前
实现Linux的ssh免密登录实操保姆级教程
linux·ssh·生成ssh密钥对的三种方法·添加公钥到需ssh免登录服务器·测试ssh免登录的服务器·生产环境linux的优化策略
zhangrelay8 小时前
操作系统全解析:Windows、macOS与Linux的深度对比与选择指南(AI)
linux·笔记·学习
阿方索9 小时前
Linux 正则表达式
linux·运维
金色熊族10 小时前
ubuntu20.04编译qt源码5.15.3
linux·c++·qt
zhaotiannuo_199811 小时前
【Linux CentOS 7 版本更换yum源】
linux
利刃大大11 小时前
【高并发服务器:前置知识】一、项目介绍 && 模块划分
运维·服务器·高并发·项目·cpp
aitav012 小时前
⚡ WSL2 搭建 s5p6818 Linux 嵌入式开发平台(part 1):环境准备与架构设计
linux·嵌入式·wsl·wsl2