开发者

Python: CTypes memory leak with Structure

开发者 https://www.devze.com 2023-04-05 06:40 出处:网络
Does Python ctypes have a known memory leak?I am working on a Python script having code like the below snippet, using ctypes, that for some reason is causing a memory leak.The 开发者_如何学Go\"while T

Does Python ctypes have a known memory leak? I am working on a Python script having code like the below snippet, using ctypes, that for some reason is causing a memory leak. The 开发者_如何学Go"while True" in this example is to test for the leak caused by calling the function. It is being run on Windows with Python 2.5.4:

import ctypes
def hi():
    class c1(ctypes.Structure):
            _fields_=[('f1',ctypes.c_uint8)]
    class c2(ctypes.Structure):
            _fields_=[('g1',c1*2)]

while True:
    test=hi()

The leak can be tested using ProcessExplorer -- as it keeps looping, Python keeps taking up more and more memory. It seems to require having two Structure subclasses where one of the classes has a "multiple" of the other one (using the * operator), but I'm not sure if the condition is more basic than that. Even if del test is added in the loop, it still leaks memory.

Any ideas on what might be causing this?

Edit: Because someone suggested it might not have garbage-collected yet, here is an edited version that does garbage-collect but still appears to leak memory:

import gc
import ctypes
def hi():
    class c1(ctypes.Structure):
            _fields_=[('f1',ctypes.c_uint8)]
    class c2(ctypes.Structure):
            _fields_=[('g1',c1*2)]

while True:
    test=hi()
    test2=gc.collect()


That's not a memory leak, that just means the garbage collector hasn't run yet. And even if the garbage collector does run, odds are good that there's some kind of memory pooling going on.

ProcessExplorer isn't a good debugging tool, especially for memory.


The script in and by itself doesn't leak. Running with gc.set_debug(gc.DEBUG_LEAK) shows that the created structure types are collectable, and gc.garbage remains empty in every loop iterations, so there are not uncollectable objects. Running the script with time on a Linux system doesn't show an steady increase in memory consumption, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号