Jul-22-2018, 05:07 PM
Consider this code:
I am running Python 2.7, although I see the same behavior with Python 3.
import gc for i in range(10): gc.collect() if True: all_objects = gc.get_objects() print "Number of objects =", len(all_objects) else: print "Number of objects =", len(gc.get_objects())With the True block, the number of objects increments by 1 on every iteration (except the first). With the False block, it does not. The result of the get_objects function call has one reference (by all_objects), but on the next iteration all_objects gets bound to a new result. At that point, I don't see who is still holding a reference to the first result, so it isn't clear to me why the number of objects keeps climbing. If it is gc itself that is holding a reference, then I don't see what good gc is for debugging a memory leak if it is causing a memory leak itself.
I am running Python 2.7, although I see the same behavior with Python 3.