【www.bbyears.com--报表/图形】
在Mac上,查看系统CPU、内存等使用量,可以用Activity Monitor这个图形化的工具。那么,怎么用命令行来查看呢?
关于CPU的使用,用top命令即可。
关于内存的使用情况,也可以用top,不过我常用的完整命令是:top -l 1 | head -n 10 | grep PhysMem
不过,关于内存的使用,我更喜欢用 vm_stat命令。
当然,vm_stat的命令输出,还不够直观,网上找了一个Python脚本来对vm_stat和ps命令的输出做了解析和包装,然后就可以输出比较简单易读的系统内存使用情况了。
命令演示如下:
PhysMem: 3192M used (703M wired), 83M unused.
jay@Jay-Air:~ $vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 15663.
Pages active: 345093.
Pages inactive: 293876.
Pages speculative: 6081.
Pages throttled: 0.
Pages wired down: 177821.
Pages purgeable: 82724.
"Translation faults": 339092568.
Pages copy-on-write: 6225304.
Pages zero filled: 216793000.
Pages reactivated: 139210195.
Pages purged: 7287795.
File-backed pages: 54755.
Anonymous pages: 590295.
Pages stored in compressor: 1239304.
Pages occupied by compressor: 209538.
Decompressions: 40056861.
Compressions: 48433378.
Pageins: 5894721.
Pageouts: 208954.
Swapins: 3541954.
Swapouts: 4157760.
Python脚本获取系统内存使用量:(https://github.com/smilejay/python/blob/master/py2014/mac_free.py)
"""
Created on Jun 1, 2014
@author: jay
"""
import subprocess
import re
# Get process info
ps = subprocess.Popen(["ps", "-caxm", "-orss,comm"], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen(["vm_stat"], stdout=subprocess.PIPE).communicate()[0]
# Iterate processes
processLines = ps.split("n")
sep = re.compile("[s]+")
rssTotal = 0 # kB
for row in range(1,len(processLines)):
rowText = processLines[row].strip()
rowElements = sep.split(rowText)
try:
rss = float(rowElements[0]) * 1024
except:
rss = 0 # ignore...
rssTotal += rss
# Process vm_stat
vmLines = vm.split("n")
sep = re.compile(":[s]+")
vmStats = {}
for row in range(1,len(vmLines)-2):
rowText = vmLines[row].strip()
rowElements = sep.split(rowText)
vmStats[(rowElements[0])] = int(rowElements[1].strip(".")) * 4096
print "Wired Memory:tt%d MB" % ( vmStats["Pages wired down"]/1024/1024 )
print "Active Memory:tt%d MB" % ( vmStats["Pages active"]/1024/1024 )
print "Inactive Memory:t%d MB" % ( vmStats["Pages inactive"]/1024/1024 )
print "Free Memory:tt%d MB" % ( vmStats["Pages free"]/1024/1024 )
print "Real Mem Total (ps):t%.3f MB" % ( rssTotal/1024/1024 )
使用该Python脚本的输出如下:
Wired Memory: 665 MB
Active Memory: 1464 MB
Inactive Memory: 1110 MB
Free Memory: 77 MB
Real Mem Total (ps): 2431.688 MB
另外,对于Mac OS X中的内存使用情况,wired, active, inactive, free等这些名词的意义,找了如下的资料:
简单的说,Mac OS X的[内存]使用情况分为:wired, active, inactive和free四种。
wired是系统核心占用的,永远不会从系统物理[内存]种驱除。
active表示这些[内存]数据正在使用中,或者刚被使用过。
inactive表示这些[内存]中的数据是有效的,但是最近没有被使用。
free, 表示这些[内存]中的数据是无效的,这些空间可以随时被程序使用。
当free的[内存]低于某个值(这个值是由你的物理[内存]大小决定的),系统则会按照以下顺序使用inactive的资源。首先 如果inactive的数据最近被调用了,系统会把它们的状态改变成active,并接在原有active[内存]逻辑地址的后面, 如果inactive的 [内存]数据最近没有被使用过,但是曾经被更改过而还没有在硬盘的相应虚拟[内存]中做修改,系统会对相应硬盘的虚拟[内存]做修改,并把这部分物理[内存]释放为free供程序使用。如果inactive[内存]中得数据被在映射到硬盘后再没有被更改过, 则直接释放成free。最后如果active的[内存]一段时间没有被使用,会被暂时改变状态为inactive。 所以说,如果你的系统里有少量的free memeory和大量的inactive的memeory,说明你的[内存]是够用的,系统运行在最佳状态,只要需要,系统就会使用它们,不用担心。而反之如果系统的free memory和inactive memory都很少,而active memory 很多,说明你的[内存]不够了。当然一开机,大部分[内存]都是free,这时系统反而不在最佳状态,因为很多数据都需要从硬盘调用,速度反而慢了。