Package 

Class LeakCanary.Config


  • 
    public final class LeakCanary.Config
    
                        

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

    • Constructor Detail

      • LeakCanary.Config

        LeakCanary.Config(Boolean dumpHeap, Boolean dumpHeapWhenDebugging, Integer retainedVisibleThreshold, List<ReferenceMatcher> referenceMatchers, List<ObjectInspector> objectInspectors, MetadataExtractor metadataExtractor, Boolean computeRetainedHeapSize, Integer maxStoredHeapDumps, Boolean requestWriteExternalStoragePermission, LeakingObjectFinder leakingObjectFinder, HeapDumper heapDumper, List<EventListener> eventListeners, Boolean showNotifications)
    • Method Detail

      • getDumpHeap

         final Boolean getDumpHeap()

        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).

        Defaults to true.

      • getDumpHeapWhenDebugging

         final Boolean getDumpHeapWhenDebugging()

        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).

        Defaults to false.

      • getRetainedVisibleThreshold

         final Integer getRetainedVisibleThreshold()

        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.

        When the app becomes invisible, LeakCanary dumps the heap after AppWatcher.retainedDelayMillis ms.

        The app is considered visible if it has at least one activity in started state.

        A higher threshold means LeakCanary will dump the heap less often, therefore it won't be bothering developers as much but it could miss some leaks.

        Defaults to 5.

      • getReferenceMatchers

         final List<ReferenceMatcher> getReferenceMatchers()

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

        When adding your own custom LibraryLeakReferenceMatcher instances, you'll most likely want to set LibraryLeakReferenceMatcher.patternApplies with a filter that checks for the Android OS version and manufacturer. The build information can be obtained by calling shark.AndroidBuildMirror.fromHeapGraph.

        Defaults to AndroidReferenceMatchers.appDefaults

      • getObjectInspectors

         final List<ObjectInspector> getObjectInspectors()

        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.

        Defaults to AndroidObjectInspectors.appDefaults

      • getMetadataExtractor

         final MetadataExtractor getMetadataExtractor()

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

        Defaults to AndroidMetadataExtractor

      • getComputeRetainedHeapSize

         final Boolean getComputeRetainedHeapSize()

        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).

        Computing the retained heap size can slow down the analysis because it requires navigating from GC roots through the entire object graph, whereas shark.HeapAnalyzer would otherwise stop as soon as all leaking instances are found.

        Defaults to true.

      • getMaxStoredHeapDumps

         final Integer getMaxStoredHeapDumps()

        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.

        Defaults to 7.

      • getRequestWriteExternalStoragePermission

         final Boolean getRequestWriteExternalStoragePermission()

        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.

        Defaults to false because that permission notification can be annoying.

      • getLeakingObjectFinder

         final LeakingObjectFinder getLeakingObjectFinder()

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

        Defaults to KeyedWeakReferenceFinder which finds all objects tracked by a KeyedWeakReference, ie all objects that were passed to ObjectWatcher.expectWeaklyReachable.

        You could instead replace it with a FilteringLeakingObjectFinder, which scans all objects in the heap dump and delegates the decision to a list of FilteringLeakingObjectFinder.LeakingObjectFilter. This can lead to finding more leaks than the default and shorter leak traces. This also means that every analysis during a given process life will bring up the same leaking objects over and over again, unlike when using KeyedWeakReferenceFinder (because KeyedWeakReference instances are cleared after each heap dump).

        The list of filters can be built from AndroidObjectInspectors:

        LeakCanary.config = LeakCanary.config.copy(
            leakingObjectFinder = FilteringLeakingObjectFinder(
                AndroidObjectInspectors.appLeakingObjectFilters
            )
        )
      • getHeapDumper

         final HeapDumper getHeapDumper()

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

      • getEventListeners

         final List<EventListener> getEventListeners()

        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:

        // No cute canary toast (very sad!)
        LeakCanary.config = LeakCanary.config.run {
          copy(
            eventListeners = eventListeners.filter {
              it !is ToastEventListener
            }
          )
        }
      • getShowNotifications

         final Boolean getShowNotifications()

        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.

        Defaults to true.