Python 脚本知道它使用了多少内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1240581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:45:34  来源:igfitidea点击:

Python script knows how much memory it's using

pythonmemory-management

提问by ???u

How can a python script know the amount of system memory it's currently using? (assuming a unix-based OS)

python 脚本如何知道它当前使用的系统内存量?(假设是基于 Unix 的操作系统)

回答by Martin v. L?wis

If you want to know the total memory that the interpreter uses, on Linux, read /proc/self/statm.

如果您想知道解释器使用的总内存,请在 Linux 上阅读/proc/self/statm.

If you want to find out how much memory your objects use, use Pympler.

如果您想了解您的对象使用了多少内存,请使用Pympler

回答by Alex

Similar question:

类似问题:

Python memory profiler

Python内存分析器

Looks like there are memory profilers for python.

看起来有 python 的内存分析器。

PySizer seems popular. Heapy is another.

PySizer 似乎很受欢迎。Heapy 是另一个。

Google: "python memory profiler" for more.

谷歌:“python 内存分析器”了解更多。

回答by Chris Ciesielski

I've used once snippet I found on ActiveStateand it seemed to work well. Actually it's using same method Martin v. L?wis suggested.

我曾经使用过在ActiveState 上找到的代码片段 ,它似乎运行良好。实际上,它使用的是 Martin v. L?wis 建议的相同方法。

回答by Nelson

I don't think there's a simple way to do this. As a practical matter, on a Unix OS I'd probably do something with os.getpid() and calling ps or reading entries in /proc.

我不认为有一种简单的方法可以做到这一点。实际上,在 Unix 操作系统上,我可能会使用 os.getpid() 并调用 ps 或读取 /proc 中的条目。

Python 2.6 adds sys.getsizeof(), which you could use in concert with gc.get_objects() to walk the size of the working set of objects:

Python 2.6 添加了 sys.getsizeof(),您可以将其与 gc.get_objects() 结合使用以遍历工作对象集的大小:

>>> print sum([sys.getsizeof(o) for o in gc.get_objects()])
561616

I don't think that'd be a good idea in practice.

我认为这在实践中不是一个好主意。

回答by Jim

I haven't used it, but you might take a look at heapy(http://guppy-pe.sourceforge.net/#Heapy), which looks to be a memory profiler for python programs.

我没有用过它,但你可以看看heapy( http://guppy-pe.sourceforge.net/#Heapy),它看起来是 Python 程序的内存分析器。