How to perform before and after diff between leaking objects
viper7882 opened this issue · 1 comments
Hi @mgedmin,
Referring to https://mg.pov.lt/objgraph/objgraph.html#objgraph.get_leaking_objects, I've discovered that get_leaking_objects API has already returned leaking objects through import of external library not sourcing from my code.
My thought is to ignore these externally leaked object but focus on any new leaked object my code has introduced. Hence I'm thinking to call get_leaking_objects twice: once before my code runs and once after my code runs.
As you've mentioned in the documentation, get_leaking_objects could report thousands of leaked object. I would like to check with you how do I perform a diff between before and after get_leaking_objects and then only dump leaking objects in the diff?
It's a very good question. I don't have an answer off-hand.
Something like
seen = {id(obj) for obj in objgraph.get_leaking_objects()}
...
new_leaks = [obj for obj in objgraph.get_leaking_objects() if id(obj) not in seen]
might work? There's a risk of id() values being reused if old objects are freed and new ones get allocated in their place, so you may miss some actual leaks. If that's important, you can keep the old objects alive by changing the first line to
seen = {id(obj): obj for obj in objgraph.get_leaking_objects()}
...
new_leaks = [obj for obj in objgraph.get_leaking_objects() if id(obj) not in seen]
at the price of some additional RAM usage.