Config

data class Config(dumpHeap: Boolean, dumpHeapWhenDebugging: Boolean, retainedVisibleThreshold: Int, referenceMatchers: List<ReferenceMatcher>, objectInspectors: List<ObjectInspector>, onHeapAnalyzedListener: OnHeapAnalyzedListener, metadataExtractor: MetadataExtractor, computeRetainedHeapSize: Boolean, maxStoredHeapDumps: Int, requestWriteExternalStoragePermission: Boolean, leakingObjectFinder: LeakingObjectFinder, heapDumper: HeapDumper, eventListeners: List<EventListener>, useExperimentalLeakFinders: Boolean)

LeakCanary configuration data class. Properties can be updated via copy.

See also

Constructors

Config
Link copied to clipboard
fun Config(dumpHeap: Boolean = true, dumpHeapWhenDebugging: Boolean = false, retainedVisibleThreshold: Int = 5, referenceMatchers: List<ReferenceMatcher> = AndroidReferenceMatchers.appDefaults, objectInspectors: List<ObjectInspector> = AndroidObjectInspectors.appDefaults, onHeapAnalyzedListener: OnHeapAnalyzedListener = DefaultOnHeapAnalyzedListener.create(), metadataExtractor: MetadataExtractor = AndroidMetadataExtractor, computeRetainedHeapSize: Boolean = true, maxStoredHeapDumps: Int = 7, requestWriteExternalStoragePermission: Boolean = false, leakingObjectFinder: LeakingObjectFinder = KeyedWeakReferenceFinder, heapDumper: HeapDumper = AndroidDebugHeapDumper, eventListeners: List<EventListener> = listOf( LogcatEventListener, ToastEventListener, LazyForwardingEventListener { if (InternalLeakCanary.formFactor == TV) TvEventListener else NotificationEventListener }, when { RemoteWorkManagerHeapAnalyzer.remoteLeakCanaryServiceInClasspath -> RemoteWorkManagerHeapAnalyzer WorkManagerHeapAnalyzer.validWorkManagerInClasspath -> WorkManagerHeapAnalyzer else -> BackgroundThreadHeapAnalyzer } ), useExperimentalLeakFinders: Boolean = false)

Types

Builder
Link copied to clipboard
class Builder

Builder for LeakCanary.Config intended to be used only from Java code.

Functions

newBuilder
Link copied to clipboard
fun newBuilder(): LeakCanary.Config.Builder

Construct a new Config via LeakCanary.Config.Builder. Note: this method is intended to be used from Java code only. For idiomatic Kotlin use copy() to modify LeakCanary.config.

Since Kotlin

999.9

Properties

computeRetainedHeapSize
Link copied to clipboard
val computeRetainedHeapSize: Boolean = true

Whether to compute the retained heap size, which is the total number of bytes in memory that would be reclaimed if the detected leaks didn't happen. This includes native memory associated to Java objects (e.g. Android bitmaps).

dumpHeap
Link copied to clipboard
val dumpHeap: Boolean = true

Whether LeakCanary should dump the heap when enough retained instances are found. This needs to be true for LeakCanary to work, but sometimes you may want to temporarily disable LeakCanary (e.g. for a product demo).

dumpHeapWhenDebugging
Link copied to clipboard
val dumpHeapWhenDebugging: Boolean = false

If dumpHeapWhenDebugging is false then LeakCanary will not dump the heap when the debugger is attached. The debugger can create temporary memory leaks (for instance if a thread is blocked on a breakpoint).

eventListeners
Link copied to clipboard
val eventListeners: List<EventListener>

Listeners for LeakCanary events. See EventListener.Event for the list of events and which thread they're sent from. You most likely want to keep this list and add to it, or remove a few entries but not all entries. Each listener is independent and provides additional behavior which you can disable by not excluding it:

heapDumper
Link copied to clipboard
val heapDumper: HeapDumper

Dumps the Java heap. You may replace this with your own implementation if you wish to change the core heap dumping implementation.

leakingObjectFinder
Link copied to clipboard
val leakingObjectFinder: LeakingObjectFinder

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

maxStoredHeapDumps
Link copied to clipboard
val maxStoredHeapDumps: Int = 7

How many heap dumps are kept on the Android device for this app package. When this threshold is reached LeakCanary deletes the older heap dumps. As several heap dumps may be enqueued you should avoid going down to 1 or 2.

metadataExtractor
Link copied to clipboard
val metadataExtractor: MetadataExtractor

Extracts metadata from a hprof to be reported in HeapAnalysisSuccess.metadata. Called on a background thread during heap analysis.

objectInspectors
Link copied to clipboard
val objectInspectors: List<ObjectInspector>

List of ObjectInspector that provide LeakCanary with insights about objects found in the heap. You can create your own ObjectInspector implementations, and also add a shark.AppSingletonInspector instance created with the list of internal singletons.

onHeapAnalyzedListener
Link copied to clipboard
val onHeapAnalyzedListener: OnHeapAnalyzedListener

Deprecated, add to LeakCanary.config.eventListeners instead. Called on a background thread when the heap analysis is complete. If you want leaks to be added to the activity that lists leaks, make sure to delegate calls to a DefaultOnHeapAnalyzedListener.

referenceMatchers
Link copied to clipboard
val referenceMatchers: List<ReferenceMatcher>

Known patterns of references in the heap, added here either to ignore them (IgnoredReferenceMatcher) or to mark them as library leaks (LibraryLeakReferenceMatcher).

requestWriteExternalStoragePermission
Link copied to clipboard
val requestWriteExternalStoragePermission: Boolean = false

LeakCanary always attempts to store heap dumps on the external storage if the WRITE_EXTERNAL_STORAGE is already granted, and otherwise uses the app storage. If the WRITE_EXTERNAL_STORAGE permission is not granted and requestWriteExternalStoragePermission is true, then LeakCanary will display a notification to ask for that permission.

retainedVisibleThreshold
Link copied to clipboard
val retainedVisibleThreshold: Int = 5

When the app is visible, LeakCanary will wait for at least retainedVisibleThreshold retained instances before dumping the heap. Dumping the heap freezes the UI and can be frustrating for developers who are trying to work. This is especially frustrating as the Android Framework has a number of leaks that cannot easily be fixed.

useExperimentalLeakFinders
Link copied to clipboard
val useExperimentalLeakFinders: Boolean = false

Deprecated: This is a no-op, set a custom leakingObjectFinder instead.