Linux查看某个用户使用总内存脚本

在之前记录过Valgrind的安装与使用

有时想要测试某个linux用户使用的总内存是否在增长,可以通过下面的python代码每隔一段时间做个统计

python 复制代码
import psutil
import time

# 指定要监视的用户名
target_user = "yifan.wang"  # 替换为要监视的用户名

# 获取初始内存使用情况
initial_memory = psutil.virtual_memory().used

while True:
    # 获取指定用户的所有进程
    user_processes = [p for p in psutil.process_iter(attrs=['pid', 'username']) if p.info['username'] == target_user]

    # 计算用户进程的内存使用情况总和
    user_memory = sum([p.memory_info().rss for p in user_processes])

    # 输出内存使用情况
    print(f"Total memory used by {target_user}: {user_memory / (1024 * 1024):.2f} MB")

    # 等待一段时间
    time.sleep(60)  # 等待60秒

    # 获取新的内存使用情况
    new_memory = psutil.virtual_memory().used

    # 检查内存是否增长
    if new_memory > initial_memory:
        print(f"Memory has increased from {initial_memory / (1024 * 1024):.2f} MB to {new_memory / (1024 * 1024):.2f} MB")
    else:
        print("Memory has not increased.")

    # 更新初始内存使用情况
    initial_memory = new_memory
相关推荐
My is 李豆1 小时前
CentOS 7 安装 Docker 完整教程(含 docker-compose 插件)
docker·eureka·centos
极客先躯1 小时前
高级java每日一道面试题-2026年02月01日-实战篇[Docker]-Docker Volume 的生命周期管理是怎样的?
java·运维·docker·容器·持久化·架构图·容器卷
kong@react2 小时前
Rocky Linux 10.2 全面解析:企业级 CentOS 替代方案及保姆级docker安装
java·linux·运维·docker
某林2123 小时前
Isaac Sim 5.1.0 无头服务器部署与 RTX 显存段错误排障全记录
运维·服务器·docker·容器·isaac
m0_738120723 小时前
Docker 环境下 Vulfocus 靶场搭建全流程(附镜像源问题解决方案)
运维·服务器·网络·安全·docker·容器
simeple4 小时前
记一次 Docker Compose 项目迁移:从 Windows Docker Desktop 迁移到 CentOS 服务器
docker
哆啦A梦——4 小时前
Ubuntu 虚拟机 Docker 与 MySQL 8.0.42 部署指南
mysql·ubuntu·docker
木雷坞5 小时前
Playwright MCP Docker 部署:mcr 镜像、浏览器工具和权限配置
运维·docker·容器·mcp
das2m5 小时前
WSL2 Ubuntu 配置完美版 docker compose 指南
linux·ubuntu·docker
暮云星影5 小时前
个人总结 docker搭建私人照片云相册Immich
docker·容器·arm