如果出现性能问题,找到占用系统内存的进程或者应用程序非常重要。
这篇文章列出了一些在一般 Linux 系统上计算内存使用情况的基本命令。
4. 检查 10 个消耗 RSS(常驻集大小)的顶级进程
请使用使用 ps 命令的简单 shell 语法:
# ps -e -orss,pid=,user=,args=, | sort -b -k1,1n | pr -TW$COLUMNS| tail -10 74336 5509 root /opt/perf/bin/perfalarm 77212 8389 oracle /grid/CRS/bin/ohasd.bin reboot 78216 9731 root /opt/OV/lbin/agtrep/agtrep -start 96768 9808 root /opt/OV/hpcs/hpsensor 108580 12073 root /usr/lib/systemd/systemd-journald 136260 76477 root storapid start -name storapid 178316 5350 root /opt/OV/bin/oacore oacore /var/opt/OV/conf/oa/PipeDefinitions/oacore.xml 186168 10367 oracle /grid/CRS/bin/oraagent.bin 1013484 31562 root vxconfigd -x syslog 1317360 76463 root vxencryptd
第一列是 RSS,第二列是 PID,第三列是 USER,第四列是执行的命令。
RSS 值是这里最重要的值,因为它显示了进程实际分配了多少内存(以 KB 为单位),请不要使用 VSZ 值,它会按进程计算“请求”内存,因为每个进程都在分配 VSZ 提供的“开销”值,其中 RSS 按进程报告真实的内存分配。
在 RSS 输出计算之后,可以从 free 命令的输出中获得 'used' 内存并与 -/+ buffers/cache used 列进行比较——RSS 和 free 命令的输出都应该匹配。
www. On IT Road .com
3. 找出占用系统内存最多的10个用户
请使用使用 ps 命令的简单 shell 语法:
# ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[]++; cpu[] += ; mem[] += } END{printf("NPROC\tUSER\tCPU\tMEM\n"); for (user in cpu) printf("%d\t%s\t%.2f\t%.2f\n",num[user], user, cpu[user], mem[user]) }' NPROC USER CPU MEM 29 oracle 0.10 4.00 1 ntp 0.00 0.00 1 rpc 0.00 0.00 5 user 0.00 0.00 1 dbus 0.00 0.00 1 mysql 0.00 0.30 12 daemon 0.00 0.60 2 postfix 0.00 0.00 12 apache1 2.40 4.80 1 rpcuser 0.00 0.00 349 root 7.70 3.30
从上面的示例观察,root 有 349 个进程占用 3.3% 的内存,用户 oracle 有 29 个进程占用 4% 的内存,用户 apache1 有 12 个进程占用了大约 4.8% 的内存,总共大约 12%。
在 sar 'commit' 列上也可以看到大约 12% 的值。
2.使用free命令计算内存使用量
# free -m total used free shared buffers cached Mem: 32106 30382 1723 3031 373 23273 -/+ buffers/cache: 6736 25370 Swap: 63999 2 63997
从上面的示例中,要获得系统上的可用内存,请使用:
-/+ buffers/cache ==> free column
免费列报告 25370 MB,以便匹配 sar 输出计算(请记住,在 SAR 中使用了平均值)
要在系统使用中获取已用内存:
-/+ buffers/cache ==> used column
在我们的例子中,使用的内存是 6736MB。
1. 使用 SAR 计算内存使用量
# sar -r 1 10 Linux 2.6.32-504.el6.x86_64 (onitroad) 03/05/2018 _x86_64_ (16 CPU) 10:17:41 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit 10:17:42 AM 1772488 31104712 94.61 382184 23831756 11675188 11.86 10:17:43 AM 1777348 31099852 94.59 382184 23831756 11675188 11.86 10:17:44 AM 1778412 31098788 94.59 382184 23831756 11675188 11.86 10:17:45 AM 1776720 31100480 94.60 382184 23831756 11675072 11.86 10:17:46 AM 1777932 31099268 94.59 382184 23831760 11675072 11.86 10:17:47 AM 1780848 31096352 94.58 382184 23831760 11675072 11.86 10:17:48 AM 1774460 31102740 94.60 382184 23831760 11675644 11.86 10:17:49 AM 1740080 31137120 94.71 382184 23831760 11717568 11.91 10:17:50 AM 1773608 31103592 94.61 382184 23831708 11675076 11.86 10:17:51 AM 1774752 31102448 94.60 382184 23831796 11675076 11.86 Average: 1772665 31104535 94.61 382184 23831757 11679414 11.87
要从平均值计算可用内存,请使用以下公式:
kbmemfree + kbbuffers + kbcached = actual free memory on the system
使用上面的例子:
1772665 + 382184 + 23831757 = 25986606KB
这大约是 24.78GB 的可用内存。
日期:2020-09-17 00:13:10 来源:oir作者:oir