Después de algo de frustración, creo que tengo la respuesta.
vm_stat es solo una parte de la imagen, también necesitamos:
$ sysctl vm.page_pageable_internal_count
La memoria de la aplicación es entonces:
page_pageable_internal_count - Pages Purgable (de vm_stat)
Esta secuencia de comandos de Python obtiene todo vm_stat, bits de sysctl, lo imprime todo a la perfección. Las últimas 6 líneas dan los mismos números que el Monitor de actividad.
#!/usr/bin/python
import sys
import subprocess
import re
f = 0.00000000093132257 # 1/(1024*1024*1024) Converts bytes to GB
# vm_stat (get all results)
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]
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
# sysctl (just get a couple of numbers)
sy = subprocess.Popen(['sysctl','vm.page_pageable_internal_count'], stdout=subprocess.PIPE).communicate()[0]
p1 = sy.find(':')
page_pageable_internal_count = float(sy[p1+1:50]) * 4096
sy = subprocess.Popen(['sysctl','vm.swapusage'], stdout=subprocess.PIPE).communicate()[0]
p1 = sy.find('used')
p2 = sy.find('M',p1)
swapUsed = float(sy[p1+7:p2]) # This is Mbytes
appMemory = page_pageable_internal_count - vmStats["Pages purgeable"]
print 'Wired Memory:\t\t%9.3f GB' % ( vmStats["Pages wired down"] * f )
print 'Active Memory:\t\t%9.3f GB' % ( vmStats["Pages active"] * f )
print 'Inactive Memory:\t%9.3f GB' % ( vmStats["Pages inactive"] * f )
print 'Speculative:\t\t%9.3f GB' % ( vmStats["Pages speculative"] * f )
print 'Throttled:\t\t%9.3f GB' % ( vmStats["Pages throttled"] * f )
print 'Free Memory:\t\t%9.3f GB' % ( vmStats["Pages free"] * f )
print 'Compressor:\t\t%9.3f GB' % ( vmStats["Pages occupied by compressor"] * f )
print 'Total:\t\t\t%9.3f GB' % ( (vmStats["Pages free"] + vmStats["Pages wired down"] + vmStats["Pages active"] + vmStats["Pages inactive"] + vmStats["Pages speculative"] + vmStats["Pages throttled"] + vmStats["Pages occupied by compressor"]) * f )
print ''
print 'Compressed:\t\t%9.3f GB' % ( vmStats["Pages stored in compressor"] * f )
print 'Purgeable:\t\t%9.3f GB' % ( vmStats["Pages purgeable"] * f )
print 'File-backed:\t\t%9.3f GB' % ( vmStats["File-backed pages"] * f )
print 'Anonymous:\t\t%9.3f GB' % ( vmStats["Anonymous pages"] * f )
print ''
print 'Pageable Internal:\t%9.3f GB' % (page_pageable_internal_count * f)
print ''
print 'App Memory:\t\t%9.3f GB' % ( appMemory * f )
print 'Wired Memory:\t\t%9.3f GB' % ( vmStats["Pages wired down"] * f )
print 'Compressor:\t\t%9.3f GB %9.3f MB' % ( vmStats["Pages occupied by compressor"] * f ,vmStats["Pages occupied by compressor"] * 1/(1024*1024) )
print 'Memory Used:\t\t%9.3f GB' % ( (appMemory + vmStats["Pages wired down"] + vmStats["Pages occupied by compressor"] ) * f )
print 'Cached Files:\t\t%9.3f GB' % ( (vmStats["File-backed pages"] + vmStats["Pages purgeable"]) * f )
print 'Swap Used:\t\t%9.3f GB %9.3f MB' % ( swapUsed * 0.0009765625, swapUsed )
sys.exit(0);
Los factores de falsificación extraños se deben a que un MB es 1024 * 1024 y un GB es 1024 * 1024 * 1024. ¡Un GB no es 1000 veces un MB!
He tenido la mayor parte de este script desde hace algún tiempo. La pieza final del rompecabezas se derivó de enlace