Monday, 15 April 2013

garbage collection - Determining if a memory leak is occurring in Python -



garbage collection - Determining if a memory leak is occurring in Python -

my understanding memory leak in python (> cpython 2.0 @ least) can only occur under next circumstances:

a circular reference graph contains 1 or more objects __del__ method an extension c/c++ or other native code module executes , leaks memory internally

of course, need create distinction between actual memory leak (a programme objects can never reclaimed via garbage collector or regular reference counting) versus programme runs out of memory because keeps allocating objects never die - (but remain in reach) - because reference graph connects global variable.

in order distinguish between these 2 circumstances (i.e. actual memory leak vs. programme keeps allocating collectable objects never go out of reach), can continuously phone call gc.collect() , check homecoming value 0 ?

in other words, if next programme never fails assertionerror (due assertion in thread 2) have proved there no memory leak (as defined above)?

thread 1: ... run actual application code ... thread 2: while true: num = gc.collect() assert num == 0 time.sleep(wait_time)

to clear, i'm asking if programme prove actual memory leak, defined cases (1) , (2) above not happening - realize wouldn't prove programme never run out of memory due many allocations.

this programme throw assertionerror if there no memory leak. time gc collects (gc.collect() returns nonzero in case). otoh, gc.collect() not collect refer "actual memory leaks," not reported in gc.collect() homecoming value.

in short, no, programme not observe memory leaks correctly @ all. if want grab case (1), can periodically check create sure gc.garbage empty, not grab case (2), because gc becomes involved in managing extension module's memory if module asks (and then, extent module correctly tracks owned references). need valgrind general case.

python garbage-collection cpython

No comments:

Post a Comment