python查看内存占用

有的时候需要对python程序内存占用进行监控,这个时候可以用到psutil库,Anaconda中是自带的,如果import出错,可以用pip install psutil(安装在python中)或conda install psutil(安装在Anaconda中)

#常用的:
import psutil
import os
info = psutil.virtual_memory()
print u'内存使用:',psutil.Process(os.getpid()).memory_info().rss
print u'总内存:',info.total
print u'内存占比:',info.percent
print u'cpu个数:',psutil.cpu_count()

其他内置的方法或属性还有:

boot_time
callable
collections
cpu_count
cpu_percent
cpu_stats
cpu_times
cpu_times_percent
disk_io_counters
disk_partitions
disk_usage
errno
functools
long
net_connections
net_if_addrs
net_if_stats
net_io_counters
os
pid_exists
pids
process_iter
pwd
signal
subprocess
swap_memory
sys
test
time
traceback
users
version_info
virtual_memory
wait_procs
win_service_get
win_service_iter

查看对象占用内存大小

import sys as sys

 a = [x for x in range(1000)]
 print sys.getsizeof(a)

 

转载于:https://my.oschina.net/u/2000675/blog/3063245

你可能感兴趣的:(python查看内存占用)