What can cause memory leak in python?

Possible duplicate:
Python: is it possible to have a real 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 give specific examples of code that would create an inaccessible area of โ€‹โ€‹memory that the garbage collector could not handle or is it impossible?

Any examples appreciated!

+6
source share
1 answer

You can use gc - the garbage collector module ,

gc.garbage :

The list of objects found by the collector is not available , but cannot be freed (unclaimed objects). From default, this list contains only objects with __del__() methods. [1] Objects that have __del__() methods and are part of the reference loop will cause the entire reference loop to be impractical, including objects not necessarily in the loop, but accessible only from it. Python does not automatically collect such loops because, in general, it is not possible for Python to guess the safe start order of __del__() . If you know the safe order, you can forcibly fix the problem by examining the garbage list and explicitly breaking loops due to your objects in the list. Note that these objects are kept alive even because they are in the garbage list, so they must be removed from the garbage too. For example, after breaking the loop, run del gc.garbage[:] to remove the list. In general, it is better to avoid not creating loops containing objects with __del__() . methods and garbage can be checked in this case to make sure that no such loops are created.

+3
source

All Articles