Possible Duplicate:
Python: Is it possible to have an actual memory leak in Python because of your code?
Since the python garbage collector handles detection of circular references (object A referencing Object B and Object B referencing Object A), I was wondering what could cause a memory leak in python code? Can you provide specific examples of code that would create an inaccessible region of memory that the garbage collector could not handle or is such a thing impossible?
Any examples appreciated !
You can use the gc — Garbage Collector interface module,
gc.garbage
:A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). By default, this list contains only objects with
__del__()
methods. [1] Objects that have__del__()
methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Python to guess a safe order in which to run the__del__()
methods. If you know a safe order, you can force the issue by examining the garbage list, and explicitly breaking cycles due to your objects within the list. Note that these objects are kept alive even so by virtue of being in the garbage list, so they should be removed from garbage too. For example, after breaking cycles, dodel
gc.garbage[:]
to empty the list. It’s generally better to avoid the issue by not creating cycles containing objects with__del__()
. methods, and garbage can be examined in that case to verify that no such cycles are being created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With