# square/leakcanary architecture

> Architecture diagram of the GitHub repository square/leakcanary, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/square/leakcanary
- Repository: https://github.com/square/leakcanary

## Overview

LeakCanary is an Android memory-leak detection library. Its principal workflow watches objects after lifecycle events, waits for retained objects, captures a heap dump, analyzes it, and exposes the result for inspection. The repository also includes Shark heap-analysis tools and the separate Dive heap-dump application. The sampled sources show WorkManager dispatch, event delivery, and analysis-result handling; several connections to unsampled analyzer and UI internals are omitted or marked unverified.

Main entry points: [`leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt`](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt) (Analysis worker), [`leakcanary/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt`](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt) (Event listener API), [`leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt`](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt) (Runtime coordinator), [`shark/shark/src/main/java/shark/HeapAnalyzer.kt`](https://github.com/square/leakcanary/blob/main/shark/shark/src/main/java/shark/HeapAnalyzer.kt) (Shark analyzer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_watching["Object watching"]
  node_app_watcher["App watcher<br/>[AppWatcher.kt]"]
  node_service_watcher["Service watcher<br/>[ServiceWatcher.kt]"]
  node_object_reporter["Object reporter"]
end

subgraph group_capture["LeakCanary runtime"]
  node_leakcanary_config["LeakCanary config<br/>[LeakCanary.kt]"]
  node_runtime["Runtime coordinator"]
  node_dump_control["Dump control<br/>[HeapDumpControl.kt]"]
  node_dump_trigger["Dump trigger<br/>[HeapDumpTrigger.kt]"]
  node_directory["Dump directory"]
  node_events["Event listener API<br/>[EventListener.kt]"]
end

subgraph group_analysis["Heap analysis"]
  node_workmanager["WorkManager dispatch"]
  node_remote_dispatch["Remote dispatch"]
  node_analysis_worker["Analysis worker"]
  node_debug_analyzer["Android analyzer"]
  node_heap_analyzer["Shark analyzer<br/>[HeapAnalyzer.kt]"]
  node_analysis_result["Analysis result<br/>[HeapAnalysis.kt]"]
end

subgraph group_inspection["Result inspection"]
  node_leak_db[("Leak database<br/>[LeakTable.kt]")]
  node_leak_activity["Leak viewer<br/>[LeakActivity.kt]"]
  node_ui_service["UI app service"]
  node_heap_repository["Heap repository<br/>[HeapRepository.kt]"]
end

subgraph group_tools["Heap tools"]
  node_cli["Shark CLI<br/>[Main.kt]"]
  node_dive_app["Dive desktop app<br/>[Main.kt]"]
end

node_android_app(("Android app"))

node_android_app -.->|"installs watchers"| node_app_watcher
node_service_watcher -->|"reports destruction"| node_object_reporter
node_runtime -->|"dispatches events"| node_events
node_runtime -->|"updates eligibility"| node_dump_control
node_dump_control -.->|"triggers capture"| node_dump_trigger
node_dump_trigger -.->|"stores dumps"| node_directory
node_events -->|"receives dump events"| node_workmanager
node_events -->|"receives dump events"| node_remote_dispatch
node_workmanager -->|"enqueues worker"| node_analysis_worker
node_remote_dispatch -->|"serializes input"| node_analysis_worker
node_analysis_worker -->|"runs analysis"| node_debug_analyzer
node_debug_analyzer -.->|"delegates analysis"| node_heap_analyzer
node_heap_analyzer -.->|"produces result"| node_analysis_result
node_analysis_worker -->|"sends events"| node_runtime
node_ui_service -->|"inserts analysis"| node_heap_repository
node_leak_activity -.->|"reads leak data"| node_leak_db
node_cli -.->|"offers analysis commands"| node_heap_analyzer
node_dive_app -.->|"opens heap dumps"| node_analysis_result

click node_app_watcher "https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher-android-core/src/main/java/leakcanary/AppWatcher.kt"
click node_service_watcher "https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher-android-core/src/main/java/leakcanary/ServiceWatcher.kt"
click node_object_reporter "https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher/src/main/java/leakcanary/DeletableObjectReporter.kt"
click node_leakcanary_config "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt"
click node_runtime "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt"
click node_dump_control "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpControl.kt"
click node_dump_trigger "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpTrigger.kt"
click node_directory "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/LeakDirectoryProvider.kt"
click node_events "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt"
click node_workmanager "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/WorkManagerHeapAnalyzer.kt"
click node_remote_dispatch "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/RemoteWorkManagerHeapAnalyzer.kt"
click node_analysis_worker "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt"
click node_debug_analyzer "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/AndroidDebugHeapAnalyzer.kt"
click node_heap_analyzer "https://github.com/square/leakcanary/blob/main/shark/shark/src/main/java/shark/HeapAnalyzer.kt"
click node_analysis_result "https://github.com/square/leakcanary/blob/main/shark/shark/src/main/java/shark/HeapAnalysis.kt"
click node_leak_db "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/db/LeakTable.kt"
click node_leak_activity "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/LeakActivity.kt"
click node_ui_service "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-app/src/main/java/org/leakcanary/service/LeakUiAppService.kt"
click node_heap_repository "https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-app/src/main/java/org/leakcanary/data/HeapRepository.kt"
click node_cli "https://github.com/square/leakcanary/blob/main/shark/shark-cli/src/main/java/shark/Main.kt"
click node_dive_app "https://github.com/square/leakcanary/blob/main/shark/shark-dive/shark-dive-app/src/main/java/shark/dive/app/Main.kt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_watcher,node_service_watcher,node_object_reporter toneBlue
class node_leakcanary_config,node_runtime,node_dump_control,node_dump_trigger,node_directory,node_events toneAmber
class node_workmanager,node_remote_dispatch,node_analysis_worker,node_debug_analyzer,node_heap_analyzer,node_analysis_result toneMint
class node_leak_db,node_leak_activity,node_ui_service,node_heap_repository toneRose
class node_cli,node_dive_app,node_android_app toneIndigo
```

## Components

### Object watching

- [App watcher](https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher-android-core/src/main/java/leakcanary/AppWatcher.kt): `object-watcher/object-watcher-android-core/src/main/java/leakcanary/AppWatcher.kt`
- [Service watcher](https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher-android-core/src/main/java/leakcanary/ServiceWatcher.kt): `object-watcher/object-watcher-android-core/src/main/java/leakcanary/ServiceWatcher.kt`
- [Object reporter](https://github.com/square/leakcanary/blob/main/object-watcher/object-watcher/src/main/java/leakcanary/DeletableObjectReporter.kt): `object-watcher/object-watcher/src/main/java/leakcanary/DeletableObjectReporter.kt`

### LeakCanary runtime

- [LeakCanary config](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt`
- [Runtime coordinator](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt`
- [Dump control](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpControl.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpControl.kt`
- [Dump trigger](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpTrigger.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapDumpTrigger.kt`
- [Dump directory](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/LeakDirectoryProvider.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/LeakDirectoryProvider.kt`
- [Event listener API](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt`

### Heap analysis

- [WorkManager dispatch](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/WorkManagerHeapAnalyzer.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/WorkManagerHeapAnalyzer.kt`
- [Remote dispatch](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/RemoteWorkManagerHeapAnalyzer.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/RemoteWorkManagerHeapAnalyzer.kt`
- [Analysis worker](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt`
- [Android analyzer](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/AndroidDebugHeapAnalyzer.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/AndroidDebugHeapAnalyzer.kt`
- [Shark analyzer](https://github.com/square/leakcanary/blob/main/shark/shark/src/main/java/shark/HeapAnalyzer.kt): `shark/shark/src/main/java/shark/HeapAnalyzer.kt`
- [Analysis result](https://github.com/square/leakcanary/blob/main/shark/shark/src/main/java/shark/HeapAnalysis.kt): `shark/shark/src/main/java/shark/HeapAnalysis.kt`

### Result inspection

- [Leak database](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/db/LeakTable.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/db/LeakTable.kt`
- [Leak viewer](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/LeakActivity.kt): `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/activity/LeakActivity.kt`
- [UI app service](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-app/src/main/java/org/leakcanary/service/LeakUiAppService.kt): `leakcanary/leakcanary-app/src/main/java/org/leakcanary/service/LeakUiAppService.kt`
- [Heap repository](https://github.com/square/leakcanary/blob/main/leakcanary/leakcanary-app/src/main/java/org/leakcanary/data/HeapRepository.kt): `leakcanary/leakcanary-app/src/main/java/org/leakcanary/data/HeapRepository.kt`

### Heap tools

- [Shark CLI](https://github.com/square/leakcanary/blob/main/shark/shark-cli/src/main/java/shark/Main.kt): `shark/shark-cli/src/main/java/shark/Main.kt`
- [Dive desktop app](https://github.com/square/leakcanary/blob/main/shark/shark-dive/shark-dive-app/src/main/java/shark/dive/app/Main.kt): `shark/shark-dive/shark-dive-app/src/main/java/shark/dive/app/Main.kt`

### Other components

- **Android app**

## Connections

- Android app → App watcher: installs watchers
- Service watcher → Object reporter: reports destruction (evidence: `object-watcher/object-watcher-android-core/src/main/java/leakcanary/ServiceWatcher.kt`)
- Runtime coordinator → Event listener API: dispatches events (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt`)
- Runtime coordinator → Dump control: updates eligibility (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt`)
- Dump control → Dump trigger: triggers capture
- Dump trigger → Dump directory: stores dumps
- Event listener API → WorkManager dispatch: receives dump events (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/WorkManagerHeapAnalyzer.kt`)
- Event listener API → Remote dispatch: receives dump events (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/RemoteWorkManagerHeapAnalyzer.kt`)
- WorkManager dispatch → Analysis worker: enqueues worker (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/WorkManagerHeapAnalyzer.kt`)
- Remote dispatch → Analysis worker: serializes input (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/RemoteWorkManagerHeapAnalyzer.kt`)
- Analysis worker → Android analyzer: runs analysis (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt`)
- Android analyzer → Shark analyzer: delegates analysis (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/AndroidDebugHeapAnalyzer.kt`)
- Shark analyzer → Analysis result: produces result
- Analysis worker → Runtime coordinator: sends events (evidence: `leakcanary/leakcanary-android-core/src/main/java/leakcanary/internal/HeapAnalyzerWorker.kt`)
- UI app service → Heap repository: inserts analysis (evidence: `leakcanary/leakcanary-app/src/main/java/org/leakcanary/service/LeakUiAppService.kt`)
- Leak viewer → Leak database: reads leak data
- Shark CLI → Shark analyzer: offers analysis commands
- Dive desktop app → Analysis result: opens heap dumps
