【发布时间】:2015-10-29 08:53:53
【问题描述】:
为什么隐藏(最小化)窗口后程序使用的内存减少了?
example.py:
import time
while True:
a = 2*2
a = 0
time.sleep(0.1)
在 cmd (Windows XP, Python 2.7.9) 中运行后,任务管理器中使用的内存 = 4 384 KB。最小化控制台窗口后使用的内存 = 1 544 KB
为什么会这样? 如何在不隐藏窗口的情况下修复内存?
更新: 解决方案:http://blog.in-orde.nl/content/memory-leak-using-com-objects-python-and-how-fix-it
【问题讨论】:
-
“隐藏”到底是什么意思?最小化?
-
这些信息是从哪里来的?为什么尽量减少内存使用量对您很重要?
-
tobifasc,是的,最小化
-
最小化窗口修剪进程工作集中的页数。您可以通过
SetProcessWorkingSetSize以编程方式执行此操作。 -
您可能更喜欢不依赖 PyWin32 的 ctypes 解决方案:
import ctypes;kernel32 = ctypes.WinDLL('kernel32');kernel32.GetCurrentProcess.restype = ctypes.c_void_p;kernel32.SetProcessWorkingSetSize.argtypes = (ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t);kernel32.SetProcessWorkingSetSize(kernel32.GetCurrentProcess(), -1, -1)。
标签: python windows memory console