12

I've been trying to debug a memory leak in the Coopr package using objgraph: https://gist.github.com/3855150

I have it pinned down to a _SetContainer object, but can't seem to figure out why that object is persisting in memory. Here's the result of objgraph.show_refs:

show_refs

How do I go about finding the circular reference and how can I get the the garbage collector to collect the _SetContainer instance?

I previously thought that the class itself might have a self-reference (the tuple just below the class on the right in the image above). But objgraph always shows inherited classes always as having a self-referencing tuple. You can see a very simple test case here.

ali_m
  • 71,714
  • 23
  • 223
  • 298
Adam Greenhall
  • 4,818
  • 6
  • 30
  • 31

1 Answers1

1

It's mostly guessing from the output of objgraph, but it seems that the instance is in a cycle and its class has a __del__. In this situation, the object is kept alive forever in CPython. Check it with:

import gc; gc.collect(); print gc.garbage

http://docs.python.org/library/gc.html#gc.garbage

Armin Rigo
  • 12,048
  • 37
  • 48
  • ``print gc.garbage`` returns an empty list, but ``count=gc.collect();`` is non-zero. I believe this means that there are uncollectable objects. Also, the [_SetContainer class](https://software.sandia.gov/trac/coopr/browser/coopr.pyomo/trunk/coopr/pyomo/base/sets.py) (or the [Component class](https://software.sandia.gov/trac/coopr/browser/coopr.pyomo/trunk/coopr/pyomo/base/component.py) it inherits from) doesn't have a ``__del__`` method explicitly defined. – Adam Greenhall Oct 22 '12 at 15:15
  • I take the second statement back - I just found the inherited ``__del__`` method. – Adam Greenhall Oct 22 '12 at 16:07
  • No, gc.collect() returns the number of unreachable object that have been found and collected. You might be hitting a case where gc.collect() is not run often enough? (Normally, it runs automatically from time to time.) Try doing a few calls to gc.collect() explicitly during the runtime of your program... – Armin Rigo Nov 06 '12 at 11:03