鱼C论坛

 找回密码
 立即注册
查看: 4314|回复: 9

[已解决]python判断内存占用

[复制链接]
发表于 2023-4-29 18:00:38 | 显示全部楼层    本楼为最佳答案   
要度量Python函数的内存使用情况,您可以使用 memory_profiler 这个库。首先,您需要安装这个库,使用pip进行安装:

  1. pip install memory-profiler
复制代码


然后,在您的Python代码中导入 memory_profiler 库并使用装饰器 @profile 来分析您的函数。例如:

  1. from memory_profiler import profile

  2. @profile
  3. def my_function():
  4.     # 您的函数实现

  5. if __name__ == '__main__':
  6.     my_function()
复制代码


运行带有 @profile 装饰器的函数时,它会输出内存使用情况的报告。要运行这个脚本并查看报告,您需要在命令行中使用 -m memory_profiler 参数来执行脚本:

  1. python -m memory_profiler your_script.py
复制代码


此外,还有一些其他方法可以检测内存使用情况,如 psutil 库,但 memory_profiler 对于函数级别的内存分析是非常实用的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 18:59:58 | 显示全部楼层

?你是觉得大了还是小了?

点评

1MiB = 8Mib  发表于 2023-4-29 19:13
Mib多大  发表于 2023-4-29 19:11
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 21:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表