python中带有psutil的单个进程的内存使用情况(以字节为单位) [英] Memory usage of a single process with psutil in python (in byte)

查看:702
本文介绍了python中带有psutil的单个进程的内存使用情况(以字节为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过psutil库获取Windows平台中单个进程已使用的内存量? (我不想知道百分比,我想知道字节数)

How to get the amount of memory which has been used by a single process in windows platform with psutil library? (I dont want to have the percentage , I want to know the amount in bytes)

我们可以使用:

psutil.virtual_memory().used

要查找整个操作系统的内存使用量(以字节为单位),那么每个进程如何呢?

To find the memory usage of the whole OS in bytes, but how about each single process?

谢谢

推荐答案

调用 memory_info_ex :

>>> import psutil
>>> p = psutil.Process()
>>> p.name()
'python.exe'

>>> _ = p.memory_info_ex()
>>> _.wset, _.pagefile
(11665408, 8499200)

工作集包含其他进程共享或共享的页面,因此在上面的示例中,它实际上比页面文件的提交费用大.

The working set includes pages that are shared or shareable by other processes, so in the above example it's actually larger than the paging file commit charge.

还有一个更简单的memory_info方法.返回rssvms,它们对应于wsetpagefile.

There's also a simpler memory_info method. This returns rss and vms, which correspond to wset and pagefile.

>>> p.memory_info()
pmem(rss=11767808, vms=8589312)


再举一个例子,让我们映射一些共享内存.


For another example, let's map some shared memory.

>>> import mmap
>>> m = mmap.mmap(-1, 10000000)
>>> p.memory_info()            
pmem(rss=11792384, vms=8609792)

映射的页面将需求零错误输入到工作集中.

The mapped pages get demand-zero faulted into the working set.

>>> for i in range(0, len(m), 4096): m[i] = 0xaa
...
>>> p.memory_info()                             
pmem(rss=21807104, vms=8581120)

私人副本会产生分页文件提交费用:

A private copy incurs a paging file commit charge:

>>> s = m[:]
>>> p.memory_info()
pmem(rss=31830016, vms=18604032)

这篇关于python中带有psutil的单个进程的内存使用情况(以字节为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆