今天要做一个python里面内存效率的测试,所以需要查看当前运行的python unittest的内存占用情况,查了一下发现psutil可以帮助你做这个事情,这样的代码就可以工作,需要安装psutil,我用的sudo pip install psutil:
class MemoryInfo(object): def __init__(self): self.current_process_id = os.getpid() self.current_process = Process(self.current_process_id) def usage(self): return self.current_process.get_memory_info() def print_usage(self): usage = self.usage() print '物理内存: %sKB, 虚拟内存: %sKB' % (usage[0]/1024, usage[1]/1024)
用的时候你可以实例化一个MemoryInfo()然后分多次调用它的print_usage方法。结绳记事。

