确定Python的实际内存使用率 [英] Determine Python's actual memory usage

查看:265
本文介绍了确定Python的实际内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试调试Python程序的内存使用情况(在具有CPython 2.7的Windows上).但不幸的是,我什至找不到任何方法来可靠地衡量其当前正在使用的内存量.

I am currently trying to debug the memory usage of my Python program (on Windows with CPython 2.7). But unfortunately, I can't even find any way to reliably measure the amount of memory it's currently using.

我一直在使用任务管理器/资源监视器来测量进程内存,但这似乎仅对确定峰值内存消耗有用.通常,即使在相关对象被垃圾回收后很长时间,Python仍不会减少Commit或Working Set.

I've been using the Task Manager/Resource Monitor to measure the process memory, but this appears to only be useful for determining peak memory consumption. Often times Python will not reduce the Commit or Working Set even long after the relevant objects have been garbage collected.

有没有办法找出Python实际使用了多少内存,或者失败了,以迫使它释放未使用的内存?我不希望使用任何需要重新编译插入器的东西.

Is there any way to find out how much memory Python is actually using, or failing that, to force it to free up its unused memory? I'd prefer not to use anything that would require recompiling the interperter.

一个例子,证明它没有释放未使用的内存:

An example of the behavior that proves it isn't freeing unused memory:

(after some calculations)   # 290k
gc.collect()                # still 290k
x = range(9999999)          # 444k
del x                       # 405k
gc.collect()                # 40k

推荐答案

有没有办法找出Python实际使用了多少内存,

Is there any way to find out how much memory Python is actually using,

不是从Python附带的.

Not from with-in Python.

您可以使用 sys.getsizeof ,但是无法捕获总的内存使用情况,总体占用,碎片,未使用的内存,但无法释放回操作系统.

You can get a rough idea of memory usage per object using sys.getsizeof however that doesn't capture total memory usage, overallocations, fragmentation, memory unused but not freed back to the OS.

有一个名为 Pympler 的第三方工具可以帮助进行记忆分析.此外,还有一个名为 Guppy 的编程环境,用于对象和堆内存的大小调整,分析和分析.还有一个名为 PySizer 的类似项目,该项目带有用于Python代码的内存使用状况分析器.

There is a third-party tool called Pympler that can help with memory analysis. Also, there a programming environment called Guppy for object and heap memory sizing, profiling and analysis. And there is a similar project called PySizer with a memory usage profiler for Python code.

还是失败,以迫使它释放未使用的内存?

or failing that, to force it to free up its unused memory?

没有用于强制释放内存的公共API.

There is no public API for forcing memory to be released.

这篇关于确定Python的实际内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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