简体   繁体   English

在Windows下检查Python程序的最大内存使用情况

[英]Check maximum memory usage of Python program under Windows

I would like to obtain, from within the Python program, the maximum physical memory used by a Python program (ActiveState Python 3.2, under Windows 7). 我想从Python程序中获取Python程序(Windows 7下为ActiveState Python 3.2)使用的最大物理内存。

Ideally, every 0.1 sec or so, I'd like to have memory usage polled, and if it exceeds the maximum seen so far, the maximum value (a global variable stored somewhere), updated. 理想情况下,我希望每0.1秒左右对内存使用情况进行一次轮询,如果它超过了到目前为止所看到的最大值,则将更新最大值(存储在某个位置的全局变量)。

UPDATE: 更新:

I realize my question is a close duplicate of Which Python memory profiler is recommended? 我意识到我的问题是推荐使用哪个Python内存分析器的近似副本 .

Sorry for being unclear. 抱歉,不清楚。 I have two specific problems that are not, to my knowledge, addressed by a regular memory profiler: 据我所知,我有两个常规内存分析器无法解决的特定问题:

  1. I need to see not only the memory allocated by Python, but the total memory used by the Python program (under Windows, this would include DLLs, etc.). 我不仅需要查看Python分配的内存,还需要查看Python程序使用的总内存(在Windows下,这将包括DLL等)。 In other words, under Windows, this is exactly what you'd see in the Task Manager . 换句话说,在Windows中,这正是您在“ 任务管理器”中看到的。

  2. I need to see the maximum memory rather than the memory at any given instant. 我需要查看最大内存,而不是任何给定瞬间的内存。 I can't think of a way to do that other than to place numerous memory checks all around the code whenever I think I'm allocating something large. 除了想在我分配大量内存时在代码周围放置大量内存检查之外,我想不出其他方法。

I think you could go under Control Panel, Admin Tools, Performances, click on the '+', select 'Processor' under 'Performance Object', Pool Paged Bytes or Pool Unpaged Bytes under the left column and you select your process under the right column. 我认为您可以在“控制面板”,“管理工具”,“性能”下,单击“ +”,在“性能对象”下选择“处理器”,在左列下选择“池分页的字节数”或“池未分页的字节数”,然后在右侧的位置选择进程柱。 You can generate a log file with the Performance monitor. 您可以使用“性能”监视器生成日志文件。

I did not find any way to do precisely what I want with Python. 我没有找到任何方法可以精确地完成我想要的Python。

However, in playing with Windows 7 Task Manager, I found I can add a column "Peak Working Set (Memory)". 但是,在使用Windows 7任务管理器时,我发现可以添加一列“峰值工作集(内存)”。 So if I simply pause the python program before it can exit (and catch exceptions from main() to pause for the same reason), I can see the peak memory in the task manager. 因此,如果我只是简单地在它可以退出之前暂停python程序(并出于同样的原因从main()捕获异常以暂停),那么我可以在任务管理器中看到峰值内存。

I know it's stupid (eg, it doesn't allow me to print it to a log file, do something based on how memory I'm using, etc.), but it's better than nothing. 我知道它很愚蠢(例如,它不允许我将其打印到日志文件中,根据我使用的内存方式来做某事等等),但是总比没有好。

Here's a link to a similar question: 这是一个类似问题的链接:

Which Python memory profiler is recommended? 建议使用哪个Python内存分析器?

I've used Guppy/Heapy and Meliae. 我用过孔雀鱼/堆和Meliae。 They're a little different, but I've found both are quite useful. 它们有些不同,但是我发现两者都很有用。

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

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