简体   繁体   中英

Python program memory in windows

Why after hide (minimize) the window the memory used of program is reduced?

example.py:

import time
while True:
    a = 2*2
    a = 0
    time.sleep(0.1)

after run in cmd (Windows XP, Python 2.7.9), used memory in task manager = 4 384 KB. after minimize console window used memory = 1 544 KB

Why is this happening? How to fix memory without hide window?

UPD: Solution: http://blog.in-orde.nl/content/memory-leak-using-com-objects-python-and-how-fix-it

Minimizing the window trims the number of pages in the process working set. You can do this programmatically via SetProcessWorkingSetSize . Here's an example using ctypes:

import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

def errcheck_bool(result, func, args):
    if not result:
        raise ctypes.WinError(ctypes.get_last_error())
    return args

kernel32.GetCurrentProcess.restype = wintypes.HANDLE
kernel32.SetProcessWorkingSetSize.errcheck = errcheck_bool
kernel32.SetProcessWorkingSetSize.argtypes = (wintypes.HANDLE,
                                              ctypes.c_size_t,
                                              ctypes.c_size_t)

def trim_working_set():
    hProcess = kernel32.GetCurrentProcess()
    kernel32.SetProcessWorkingSetSize(hProcess, -1, -1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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