wholecell.utils.memory_debug

Memory leak debugging utility. This should detect uncollectable Python objects but not leaked C/C++ nodes.

If the caller defaults enabled, then the OS environment variable DEBUG_GC will enable/disable memory leak detection.

Example

with detect_leaks():

make_a_matplotlib_chart()

detect_leaks() is a context manager that runs a full GC and restores GC debug flags when done. It should work fine to nest with detect_leaks(False) for a block of code inside with detect_leaks(True).

wholecell.utils.memory_debug.detect_leaks(enabled=None)[source]

A context manager that optionally detects Python object leaks in the with statement body.

Set enabled to True to enable leak detection, False to disable leak detection, or default to let the ‘DEBUG_GC’ environment variable (int, “0”) enable leak detection if non-zero.

Leak detection has some overhead including running a full collection and printing a list of uncollectable objects.

Per https://docs.python.org/2/library/gc.html, “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.”