Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to perform before and after diff between leaking objects #62

Open
viper7882 opened this issue Jun 25, 2021 · 1 comment
Open

How to perform before and after diff between leaking objects #62

viper7882 opened this issue Jun 25, 2021 · 1 comment

Comments

@viper7882
Copy link

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?

@mgedmin
Copy link
Owner

mgedmin commented Jul 1, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants