深度阅读

How to get the memory usage of a Linux system using Python ?python 获取linux内存占用

作者
作者
2023年08月22日
更新时间
8.37 分钟
阅读时间
0
阅读量

To get the memory usage of a Linux system using Python, you can use the psutil library. Here is an example code that will print the total virtual memory size, available virtual memory size, used virtual memory size, and memory usage percentage:

import psutil

# Get virtual memory usage statistics
virtual_memory = psutil.virtual_memory()

# Print memory usage statistics
print(f"Total: {virtual_memory.total}")
print(f"Available: {virtual_memory.available}")
print(f"Used: {virtual_memory.used}")
print(f"Percent: {virtual_memory.percent}")

psutil provides a lot of other system information as well, like CPU usage, process information, network usage, etc. To install psutil, you can use pip:

pip install psutil
This code should work on any Linux, as well as other operating systems that support psutil.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。