Skip to content
🤔 Documentation issue? Report it

//leakcanary/leakcanary/LeakCanary/Config

Config

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

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

See also

main

leakcanary.LeakCanary

Constructors

Config 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 } ), showNotifications: Boolean = true, useExperimentalLeakFinders: Boolean = false)

Types

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

Functions

Name Summary
newBuilder 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

Name Summary
computeRetainedHeapSize 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 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 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 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 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 val leakingObjectFinder: LeakingObjectFinder
Finds the objects that are leaking, for which LeakCanary will compute leak traces.
maxStoredHeapDumps 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 val metadataExtractor: MetadataExtractor
Extracts metadata from a hprof to be reported in HeapAnalysisSuccess.metadata. Called on a background thread during heap analysis.
objectInspectors 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.
referenceMatchers 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 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 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.
showNotifications val showNotifications: Boolean = true
Whether to show LeakCanary notifications. When showNotifications is true, LeakCanary will only display notifications if the app is in foreground and is not an instant, TV or Wear app.