English 中文(简体)
甲型六氯环己烷
原标题:Python: CTypes memory leak with Structure

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 "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()

泄漏可以使用处理程序Explorer进行检测,因为它保持了 lo,保持了越来越多的记忆。 似乎需要两个结构子类,其中一个类别有另一个类别中的“多层”(使用<代码>*的操作员),但我并不肯定这一条件是否比这一条件更为基本。 即便在路程中添加了<条码>del ,但它仍然泄露记忆。

任何关于可能产生这种情况的想法?

Edit:由于有人建议,它可能没有收集垃圾,这里是一份经过编辑的版本,它确实收集垃圾,但似乎仍然泄露记忆:

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()
问题回答

这并不是一个记忆泄露,这只是垃圾收集器的 t。 即使垃圾收集器投入运行,奇迹也好,有些记忆库在继续。

工艺外包器是一种良好的分解工具,特别是用于记忆的工具。

文字本身也无漏。 gc.set_debug(gc.DEBUG_LEAK)显示,所创建的结构类型是可收集的,gc.garbage在每一座机中仍然空洞,因此不存在无法收集的物体。 用<代码>实时<>在含水层系统中操作,也显示记忆消耗量也在稳步增加。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签