"Help needed - I don't understand how Python manages memory"

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 20 20:53:06 CEST 2008


En Sun, 20 Apr 2008 15:02:37 -0300, Torsten Bronger <bronger at physik.rwth-aachen.de> escribió:
> Gabriel Genellina writes:
>> En Sun, 20 Apr 2008 14:43:17 -0300, Christian Heimes <lists at cheimes.de>
>> escribió:
>>> Gabriel Genellina schrieb:
>>>
>>>> Apart from what everyone has already said, consider that
>>>> FreqDist may import other modules, store global state, create
>>>> other objects... whatever.  Pure python code should not have any
>>>> memory leaks (if there are, it's a bug in the Python
>>>> interpreter). Not-carefully-written C extensions may introduce
>>>> memory problems.
>>>
>>> Pure Python code can cause memory leaks. No, that's not a bug in
>>> the interpreter but the fault of the developer. For example code
>>> that messes around with stack frames and exception object can
>>> cause nasty reference leaks.
>>
>> Ouch!
>> May I assume that code that doesn't use stack frames nor stores
>> references to exception objects/tracebacks is safe?
>
> Circular referencing is no leaking on the C level but in a way it is
> memory leaking, too.

The garbage collector will eventually dispose of the cycle, unless you use __del__. Of course it's better if the code itself breaks the cycle when it's done using the objects.
The above comment about stack frames and exceptions does not refer to this situation, I presume.

-- 
Gabriel Genellina




More information about the Python-list mailing list