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

相关推荐
韩楚风23 分钟前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学26 分钟前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO32 分钟前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu71 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我1 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、1 小时前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程1 小时前
Linux中环境变量
linux
挥剑决浮云 -1 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
小O_好好学2 小时前
CentOS 7文件系统
linux·运维·centos
哲伦贼稳妥3 小时前
一天认识一个硬件之机房地板
运维·网络·经验分享·其他