main

square/leakcanary

Last updated at: 29/12/2023 09:39

FilteringLeakingObjectFinder.kt

TLDR

The FilteringLeakingObjectFinder class in the provided file is used to find objects that are leaking in the heap dump. It delegates the decision to a list of LeakingObjectFilter interfaces.

Classes

FilteringLeakingObjectFinder

The FilteringLeakingObjectFinder class is responsible for finding objects that are leaking in the heap dump. It takes a list of LeakingObjectFilter interfaces as a parameter and implements the LeakingObjectFinder interface. The class provides the following methods:

  • findLeakingObjectIds(graph: HeapGraph): Set<Long>: This method finds the leaking object ids by filtering the objects in the heap graph based on the filters provided during initialization. It returns a set of long values representing the object ids of the leaking objects.

Interfaces

LeakingObjectFilter

The LeakingObjectFilter interface is a functional interface that defines a single method for filtering whether a heap object is leaking or not. The interface provides the following method:

  • isLeakingObject(heapObject: HeapObject): Boolean: This method takes a HeapObject as a parameter and returns a boolean value indicating whether the object is leaking or not.
package shark

/**
 * Finds the objects that are leaking by scanning all objects in the heap dump
 * and delegating the decision to a list of [FilteringLeakingObjectFinder.LeakingObjectFilter]
 */
class FilteringLeakingObjectFinder(private val filters: List<LeakingObjectFilter>) :
  LeakingObjectFinder {

  /**
   * Filter to be passed to the [FilteringLeakingObjectFinder] constructor.
   */
  fun interface LeakingObjectFilter {
    /**
     * Returns whether the passed in [heapObject] is leaking. This should only return true
     * when we're 100% sure the passed in [heapObject] should not be in memory anymore.
     */
    fun isLeakingObject(heapObject: HeapObject): Boolean
  }

  override fun findLeakingObjectIds(graph: HeapGraph): Set<Long> {
    return graph.objects
      .filter { heapObject ->
        filters.any { filter ->
          filter.isLeakingObject(heapObject)
        }
      }
      .map { it.objectId }
      .toSet()
  }
}