xref: /aosp_15_r20/external/leakcanary2/shark/src/main/java/shark/FilteringLeakingObjectFinder.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)

<lambda>null1 package shark
2 
3 /**
4  * Finds the objects that are leaking by scanning all objects in the heap dump
5  * and delegating the decision to a list of [FilteringLeakingObjectFinder.LeakingObjectFilter]
6  */
7 class FilteringLeakingObjectFinder(private val filters: List<LeakingObjectFilter>) :
8   LeakingObjectFinder {
9 
10   /**
11    * Filter to be passed to the [FilteringLeakingObjectFinder] constructor.
12    */
13   fun interface LeakingObjectFilter {
14     /**
15      * Returns whether the passed in [heapObject] is leaking. This should only return true
16      * when we're 100% sure the passed in [heapObject] should not be in memory anymore.
17      */
18     fun isLeakingObject(heapObject: HeapObject): Boolean
19   }
20 
21   override fun findLeakingObjectIds(graph: HeapGraph): Set<Long> {
22     return graph.objects
23       .filter { heapObject ->
24         filters.any { filter ->
25           filter.isLeakingObject(heapObject)
26         }
27       }
28       .map { it.objectId }
29       .toSet()
30   }
31 }
32