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

Support computing retained size for any object #2642

Open
pyricau opened this issue Feb 27, 2024 · 0 comments
Open

Support computing retained size for any object #2642

pyricau opened this issue Feb 27, 2024 · 0 comments

Comments

@pyricau
Copy link
Member

pyricau commented Feb 27, 2024

When LeakCanary traverses the graph to find shortest paths to GC Roots, it has an option to also compute retained sizes for nodes in those paths. When that option is turned on, LeakCanary will traverse the entire graph (instead of bailing early once its found all leaking instances).

The graph traversal creates a DominatorTree object, used here to compute retained size:

return dominatorTree.computeRetainedSizes(nodeObjectIds) { objectId ->

I just re read the DominatorTree code and took notes

This is a destructive way of computing the retained size.
The idea is that we’re going through all objects in the heap. For each object we know its direct dominator, however, we don’t really care about that
we really care about the closest dominator in the dominator chain that also belongs to the set of objects for which we’re trying to compute retained size
that set if a very small set of objects (edited)
so we essentially updating the dominator map, instead of being object => dominator, it becomes “object => dominator we’re interested in for retained size, or nothing”
so as we go through the entire map we also recursively for each entry have to go through parent dominators, but the destructive update makes it so that over time we tend to have to do less of that

We could change this to:

  • Make that code operate on a copy of the Map<Long, Long> instead of destructing the original
  • Move DominatorTree into HeapGraph graph context (basically a cache)

That would allow us to compute retained size for any other object, after LeakCanary has done its traversal. One benefit is that we can then plug in whatever shallow size computation method we want.

However, it's a bit of a waste because we're re traversing dominators every time.

Alternatively we could compute retained size for all dominators, and store that instead.

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

No branches or pull requests

1 participant