Package-level declarations

Types

Link copied to clipboard

Creates ReferenceReader instances that will follow references from all HeapObjects, applying matching rules provided by referenceMatchers, and not creating any virtual reference.

Link copied to clipboard
class AndroidNativeSizeMapper(graph: HeapGraph)
Link copied to clipboard

Creates ReferenceReader instances that will follow references from all HeapObjects, applying matching rules provided by referenceMatchers, creating additional virtual instance reference based on known Android classes.

Link copied to clipboard
data class ApplicationLeak(val leakTraces: List<LeakTrace>) : Leak

A leak found by HeapAnalyzer in your application.

Link copied to clipboard
class AppSingletonInspector(singletonClasses: String) : ObjectInspector

Inspector that automatically marks instances of the provided class names as not leaking because they're app wide singletons.

Link copied to clipboard
value class ByteSize(val inWholeBytes: Long) : Comparable<ByteSize>

Inspired by https://github.com/saket/file-size as well as Kotlin's Duration API.

Link copied to clipboard

A ReferenceReader that first delegates expanding to virtualRefReaders in order until one matches (or none), and then always proceeds with fieldRefReader. This means any synthetic ref will be on the shortest path, but we still explore the entire data structure so that we correctly track which objects have been visited and correctly compute dominators and retained size.

Link copied to clipboard
class ClassReferenceReader(graph: HeapGraph, referenceMatchers: List<ReferenceMatcher>) : ReferenceReader<HeapObject.HeapClass>
Link copied to clipboard
Link copied to clipboard
class DominatorTree(expectedElements: Int = 4)
Link copied to clipboard
class FieldInstanceReferenceReader(graph: HeapGraph, referenceMatchers: List<ReferenceMatcher>) : ReferenceReader<HeapObject.HeapInstance>

Expands instance fields that hold non null references.

Link copied to clipboard

Finds the objects that are leaking by scanning all objects in the heap dump and delegating the decision to a list of FilteringLeakingObjectFinder.LeakingObjectFilter

Link copied to clipboard
class FirstHeapTraversal(val shortestPathTree: ShortestPathObjectNode, previousTraversal: InitialState) : HeapTraversalOutput
class FlatteningPartitionedInstanceReferenceReader(graph: HeapGraph, instanceReferenceReader: FieldInstanceReferenceReader)

FlatteningPartitionedInstanceReferenceReader provides a synthetic and stable representation of a data structure that maps how we think about that data structure instead of how it is internally implemented. You can think of it as surfacing additional direct references to entries that the data structure holds. VirtualInstanceReferenceReader implementations scan references based on known patterns rather than through generic traversals. As a result, they do not surface references and objects that are part of the data structure implementations, such as internal arrays or linked lists. This is a problem because the same traversal is also used to compute retained size, so we need to accounts for all reachable objects.

Link copied to clipboard
fun interface GcRootProvider
Link copied to clipboard
class GcRootReference(val gcRoot: GcRoot, val isLowPriority: Boolean, val matchedLibraryLeak: LibraryLeakReferenceMatcher?)
Link copied to clipboard
Link copied to clipboard
sealed class HeapAnalysis : Serializable

The result of an analysis performed by HeapAnalyzer, either a HeapAnalysisSuccess or a HeapAnalysisFailure. This class is serializable however there are no guarantees of forward compatibility.

Link copied to clipboard
Link copied to clipboard
data class HeapAnalysisFailure(val heapDumpFile: File, val createdAtTimeMillis: Long, val dumpDurationMillis: Long = DUMP_DURATION_UNKNOWN, val analysisDurationMillis: Long, val exception: HeapAnalysisException) : HeapAnalysis

The analysis performed by HeapAnalyzer did not complete successfully.

Link copied to clipboard
data class HeapAnalysisSuccess(val heapDumpFile: File, val createdAtTimeMillis: Long, val dumpDurationMillis: Long = DUMP_DURATION_UNKNOWN, val analysisDurationMillis: Long, val metadata: Map<String, String>, val applicationLeaks: List<ApplicationLeak>, val libraryLeaks: List<LibraryLeak>, val unreachableObjects: List<LeakTraceObject>) : HeapAnalysis

The result of a successful heap analysis performed by HeapAnalyzer.

Link copied to clipboard

Analyzes heap dumps to look for leaks.

Link copied to clipboard
class HeapDiff(val traversalCount: Int, val shortestPathTree: ShortestPathObjectNode, val growingObjects: GrowingObjectNodes, previousTraversal: HeapTraversalInput) : HeapTraversalOutput
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
class IgnoredReferenceMatcher(val pattern: ReferencePattern, val patternApplies: (HeapGraph) -> Boolean = ALWAYS) : ReferenceMatcher

IgnoredReferenceMatcher should be used to match references that cannot ever create leaks. The shortest path finder will never go through matching references.

Link copied to clipboard
class InitialState(val scenarioLoopsPerGraph: Int = DEFAULT_SCENARIO_LOOPS_PER_GRAPH) : HeapTraversalInput
Link copied to clipboard

Finds all objects tracked by a KeyedWeakReference, ie all objects that were passed to ObjectWatcher.watch.

Link copied to clipboard
sealed class Leak : Serializable

A leak found by HeapAnalyzer, either an ApplicationLeak or a LibraryLeak.

Link copied to clipboard
fun interface LeakingObjectFinder

Finds the objects that are leaking, for which Shark will compute leak traces.

Link copied to clipboard
data class LeaksAndUnreachableObjects(val applicationLeaks: List<ApplicationLeak>, val libraryLeaks: List<LibraryLeak>, val unreachableObjects: List<LeakTraceObject>)
Link copied to clipboard
data class LeakTrace(val gcRootType: LeakTrace.GcRootType, val referencePath: List<LeakTraceReference>, val leakingObject: LeakTraceObject) : Serializable

The best strong reference path from a GC root to the leaking object. "Best" here means the shortest prioritized path. A large number of distinct paths can generally be found leading to a leaking object. Shark prioritizes paths that don't go through known LibraryLeakReferenceMatcher (because those are known to create leaks so it's more interesting to find other paths causing leaks), then it prioritize paths that don't go through java local gc roots (because those are harder to reason about). Taking those priorities into account, finding the shortest path means there are less LeakTraceReference that can be suspected to cause the leak.

Link copied to clipboard
data class LeakTraceObject(val type: LeakTraceObject.ObjectType, val className: String, val labels: Set<String>, val leakingStatus: LeakTraceObject.LeakingStatus, val leakingStatusReason: String, val retainedHeapByteSize: Int?, val retainedObjectCount: Int?) : Serializable
Link copied to clipboard
fun interface LeakTracer
Link copied to clipboard
data class LeakTraceReference(val originObject: LeakTraceObject, val referenceType: LeakTraceReference.ReferenceType, val owningClassName: String, val referenceName: String) : Serializable

A LeakTraceReference represents an origin LeakTraceObject and either a reference from that object to the LeakTraceObject in the next LeakTraceReference in LeakTrace.referencePath, or to LeakTrace.leakingObject if this is the last LeakTraceReference in LeakTrace.referencePath.

Link copied to clipboard
data class LibraryLeak(val leakTraces: List<LeakTrace>, val pattern: ReferencePattern, val description: String) : Leak

A leak found by HeapAnalyzer, where the only path to the leaking object required going through a reference matched by pattern, as provided to a LibraryLeakReferenceMatcher instance. This is a known leak in library code that is beyond your control.

Link copied to clipboard
data class LibraryLeakReferenceMatcher(val pattern: ReferencePattern, val description: String = "", val patternApplies: (HeapGraph) -> Boolean = ALWAYS) : ReferenceMatcher

LibraryLeakReferenceMatcher should be used to match references in library code that are known to create leaks and are beyond your control. The shortest path finder will only go through matching references after it has exhausted references that don't match, prioritizing finding an application leak over a known library leak. Library leaks will be reported as LibraryLeak instead of ApplicationLeak.

Link copied to clipboard

TODO Extracted from PathFinder, this should eventually be part of public API surface and we should likely also revisit the gc root type filtering which happens during heap parsing, as that's not really a concern for the heap parser and more for path finding. There are probably memory concerns as well there though. We could:

Link copied to clipboard
fun interface MetadataExtractor

Extracts metadata from a hprof to be reported in HeapAnalysisSuccess.metadata.

Link copied to clipboard
class ObjectArrayReferenceReader : ReferenceReader<HeapObject.HeapObjectArray>
Link copied to clipboard

Exposes high level APIs to compute and render a dominator tree. This class needs to be public to be used by other LeakCanary modules but is internal and its API might change at any moment.

Link copied to clipboard
class ObjectGrowthDetector(gcRootProvider: GcRootProvider, referenceReaderFactory: ReferenceReader.Factory<HeapObject>)

Looks for objects that have grown in outgoing references in a new heap dump compared to a previous heap dump by diffing heap traversals.

Link copied to clipboard
fun interface ObjectInspector

Provides LeakCanary with insights about objects (classes, instances and arrays) found in the heap. inspect will be called for each object that LeakCanary wants to know more about. The implementation can then use the provided ObjectReporter to provide insights for that object.

Link copied to clipboard

A set of default ObjectInspectors that knows about common JDK objects.

Link copied to clipboard
class ObjectReporter(val heapObject: HeapObject)

Enables ObjectInspector implementations to provide insights on heapObject, which is an object (class, instance or array) found in the heap.

Link copied to clipboard

Reports progress from the HeapAnalyzer as they occur, as Step values.

Link copied to clipboard
Link copied to clipboard
class PathFindingResults(val pathsToLeakingObjects: List<ReferencePathNode>, val dominatorTree: DominatorTree?)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
data class Reference(val valueObjectId: Long, val isLowPriority: Boolean, val isLeafObject: Boolean = false, val lazyDetailsResolver: Reference.LazyDetails.Resolver)

TODO Review as public API.

Link copied to clipboard

TODO This is quite similar to the leaktrace equivalent

Link copied to clipboard
sealed class ReferenceMatcher

Used to pattern match known patterns of references in the heap, either to ignore them (IgnoredReferenceMatcher) or to mark them as library leaks (LibraryLeakReferenceMatcher), which lowers their traversal priority when exploring the heap.

Link copied to clipboard

A pattern that will match references for a given ReferenceMatcher.

Link copied to clipboard
fun interface ReferenceReader<T : HeapObject>
Link copied to clipboard
value class Retained
Link copied to clipboard
fun interface ShortestPathFinder
Link copied to clipboard

Creates ReferenceReader instances that will follow references from all HeapObjects, applying matching rules provided by referenceMatchers, creating additional virtual instance reference based on the list of VirtualInstanceReferenceReader created by virtualRefReadersFactory.

Functions

Link copied to clipboard
fun ObjectGrowthDetector.Companion.forJvmHeap(referenceMatchers: List<ReferenceMatcher> = JvmObjectGrowthReferenceMatchers.defaults): ObjectGrowthDetector
Link copied to clipboard
fun ReferencePattern.ignored(patternApplies: (HeapGraph) -> Boolean = ALWAYS): IgnoredReferenceMatcher
Link copied to clipboard
fun ReferencePattern.leak(description: String = "", patternApplies: (HeapGraph) -> Boolean = ALWAYS): LibraryLeakReferenceMatcher
Link copied to clipboard
inline fun Retained(heapSize: ByteSize, objectCount: Int): Retained

Constructors can't be inlined so we used a function instead.

Properties

Link copied to clipboard
Link copied to clipboard
const val BYTES_PER_GB: Long
Link copied to clipboard
const val BYTES_PER_KB: Long
Link copied to clipboard
const val BYTES_PER_MB: Long
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard