简体   繁体   English

如何获得 python 脚本的峰值 memory 用法?

[英]How to get peak memory usage of python script?

I'm doing some extensive scientific python calculations and whant to know execution time and memory footprint of python script.我正在做一些广泛的科学 python 计算,想知道 python 脚本的执行时间和 memory 足迹。

So how to get peak memory usage of python script?那么如何获取 python 脚本的峰值memory 用法呢?

If it matters I'm on Windows and use python 2.7.如果重要的话,我在 Windows 上并使用 python 2.7。

The resource module can give you this.资源模块可以给你这个。 Works in both Python 2 and Python 3.适用于 Python 2 和 Python 3。

import resource
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

This is peak memory in kilobytes .这是以千字节为单位的峰值 memory。 The user and system time is also included in the value from getrusage.用户和系统时间也包含在 getrusage 的值中。

Sounds like you are looking for a memory profiler. 听起来你正在寻找一个内存分析器。 Memory_profiler is one that you can dive into which line is giving you the problems and with some querying you can figure out which area is the biggest in memory consumption. Memory_profiler是一个你可以深入了解哪一行给你的问题,并通过一些查询你可以找出哪个区域是最大的内存消耗。

https://pypi.python.org/pypi/memory_profiler and since you are using windows it will also need this https://pypi.python.org/pypi/psutil https://pypi.python.org/pypi/memory_profiler ,因为你使用的是windows,它还需要这个https://pypi.python.org/pypi/psutil

Good Luck! 祝好运!

For the peak memory, as you are on Windows, you can use psutil and psutil.Process.memory_info , for example to get the peak working set size, in bytes: 对于峰值内存,就像在Windows上一样,您可以使用psutilpsutil.Process.memory_info来获取峰值工作集大小(以字节为单位):

>>> import psutil
>>> p = psutil.Process()
>>> p.memory_info().peak_wset
238530560L

As per the link above, you can get more details about some Windows specific fields on this page . 根据上面的链接,您可以在此页面上获得有关某些Windows特定字段的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM