[英]psutil Returning Inaccurate RAM Usage Information
I am building a hardware utilisation display into my Python project using the psutil package.我正在使用psutil package 将硬件利用率显示构建到我的 Python 项目中。
In the Python shell, I am getting the RAM usage as shown below:在 Python shell 中,我得到的 RAM 使用情况如下所示:
>>> import psutil
>>> psutil.virtual_memory()
svmem(total=17179869184, available=6488571904, percent=62.2, used=9150734336, free=20709376, active=6468239360, inactive=6167109632, wired=2682494976)
There is a large discrepancy between the total
value and the actual maximum RAM capacity of my computer, the same applies to the available
and used
values. total
与我电脑的实际最大 RAM 容量有很大差异, available
值和已used
值也是如此。
Below is a cropped screenshot of the RAM usage info in the macOS Activity Monitor app, taken not so long after the psutil.virtual_memory()
command in the Python shell.下面是 macOS 活动监视器应用程序中 RAM 使用信息的裁剪屏幕截图,在 Python shell 中的
psutil.virtual_memory()
命令之后不久拍摄。
What could be causing these discrepancies, and how do I fix this?什么可能导致这些差异,我该如何解决这个问题?
There's no discrepancy.没有差异。 Values are in bits.
值以位为单位。
You can get a human-readable version of those sizes with the humanize
library.您可以使用
humanize
库获得这些尺寸的人类可读版本。 For example, I copied some of the numbers from psutil
that you posted in your question in this script:例如,我从
psutil
复制了您在此脚本中的问题中发布的一些数字:
import humanize
print("total: " + humanize.naturalsize(17179869184, binary=True))
print("available: " + humanize.naturalsize(6488571904, binary=True))
print("used: " + humanize.naturalsize(9150734336, binary=True))
print("active: " + humanize.naturalsize(6468239360, binary=True))
print("inactive: " + humanize.naturalsize(6167109632, binary=True))
Which prints:哪个打印:
total: 16.0 GiB
available: 6.0 GiB
used: 8.5 GiB
active: 6.0 GiB
inactive: 5.7 GiB
I believe that this output matches the data shown by your UI.我相信这个 output 与您的 UI 显示的数据相匹配。
Note that most hardware vendors and hardware-related tools use the incorrect units to represent size .请注意,大多数硬件供应商和硬件相关工具使用不正确的单位来表示 size 。 Ie you should see GiB instead of GB.
即你应该看到 GiB 而不是 GB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.