mgedmin / objgraph

Visually explore Python object graphs
http://mg.pov.lt/objgraph/
MIT License
768 stars 72 forks source link

Fix reference cycle that can result from objgraph.by_type #22

Closed embray closed 9 years ago

embray commented 9 years ago

This is perhaps a bit obscure, but was still biting me when trying to debug an unrelated reference cycle. In the process of debugging I set gc.disable() so that I can ensure that I can find the (otherwise collectable) reference cycle before it's collected.

However, when running objgraph.by_type, it seems that a reference cycle can be created between the stack frame for by_type and itself by way of the objects list. Normally this would be cleaned up on the next garbage collector run, but since I'm leaving automatic garbage collection disabled it sticks around. Then when I run gc.get_referrers on one of the objects returned by by_type, I get back the huge objects list as one of its referrers since it's still being tracked.

Manually deleting the objects list at the end of by_type ensures that any reference cycles involving it are broken and can be cleaned up before any further inspection is performed on an object's referrers.

mgedmin commented 9 years ago

Thank you for the pull request!

mgedmin commented 9 years ago

Some other functions have the same problem: count(), typestats(). I'm fixing them too.

mgedmin commented 9 years ago

objgraph 2.0.1 is out with this fix.

embray commented 9 years ago

Thanks!