脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python中使用psutil查看内存占用的情况

python中使用psutil查看内存占用的情况

2021-03-03 00:39晓东邪 Python

今天小编就为大家分享一篇python中使用psutil查看内存占用的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

?
1
2
3
4
5
6
7
8
#常用的:
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()

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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

查看windows开机时间

?
1
2
3
import time
import psutil
print (u'电脑开机时间:{}'.format(time.strftime('%y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))))

以上这篇python中使用psutil查看内存占用的情况就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xiaodongxiexie/article/details/54633049

延伸 · 阅读

精彩推荐