square / leakcanary

A memory leak detection library for Android.
https://square.github.io/leakcanary
Apache License 2.0
29.4k stars 3.97k forks source link

Force Heap dump in tests to allow heap analysis #2458

Open sahil2441 opened 1 year ago

sahil2441 commented 1 year ago

Problem description

We are using heap dumps in instrumentation tests using InstrumentationLeakDetector.detectLeaks(). The idea is to leverage the heap dump analysis at the end and validate if there's an additional heap size / bitmap count etc. detected, to watchout for any potential memory regressions.

However the logic to get a heap dump trigger is tied to the fact whether a leak was found or could be found.

In many cases the heap dump doesn't get triggered because there are no objects with a weak reference after a GC etc. Even in those cases when there's no leak, we could still have a memory regression and would like to do a heap analysis.

Potential solutions

Additional information

pyricau commented 1 year ago

Thanks for the idea. The assertNoLeak API does imply a relationship with leaks :)

Have you looked into using the underlying bricks directly? Is there anything blocking you, besides having to write ad hoc code?

What would an API look like for you? It sounds like you want to ensure the heap is dumped and then you want access to the heap dump graph API?

pyricau commented 1 year ago

Something to consider for 3.0 as I look into breaking down the APIs in smaller composable pieces.