mac系统当前查看内存

 1#!/usr/bin/python
 2# -*- coding: utf-8 -*-
 3
 4'''
 5Created on Jun 1, 2014
 6@author: jay
 7'''
 8
 9import subprocess
10import re
11
12# Get process info
13ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]
14vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]
15
16# Iterate processes
17processLines = ps.split('\n')
18sep = re.compile('[\s]+')
19rssTotal = 0 # kB
20for row in range(1,len(processLines)):
21  rowText = processLines[row].strip()
22  rowElements = sep.split(rowText)
23  try:
24    rss = float(rowElements[0]) * 1024
25  except:
26    rss = 0 # ignore...
27  rssTotal += rss
28
29# Process vm_stat
30vmLines = vm.split('\n')
31sep = re.compile(':[\s]+')
32vmStats = {}
33for row in range(1,len(vmLines)-2):
34  rowText = vmLines[row].strip()
35  rowElements = sep.split(rowText)
36  vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096
37
38# print vmStats;
39
40print '有线内存:\t\t%d MB' % ( vmStats["Pages wired down"]/1024/1024 )
41print '活动内存:\t\t%d MB' % ( vmStats["Pages active"]/1024/1024 )
42print '非活动内存:\t\t%d MB' % ( vmStats["Pages inactive"]/1024/1024 )
43print '可用内存:\t\t%d MB' % ( vmStats["Pages free"]/1024/1024 )
44print '实际内存总计 (ps):\t%.3f MB' % ( rssTotal/1024/1024 )