1*d9e8da70SAndroid Build Coastguard WorkerThe fundamentals describe how LeakCanary works and how to use it to detect and fix memory leaks. This documentation is designed to help developers of all levels, so please don't hesitate to report any confusing section. 2*d9e8da70SAndroid Build Coastguard Worker 3*d9e8da70SAndroid Build Coastguard Worker## What is a memory leak? 4*d9e8da70SAndroid Build Coastguard Worker 5*d9e8da70SAndroid Build Coastguard WorkerIn a Java based runtime, a memory leak is a programming error that causes an application to keep a reference to an object that is no longer needed. As a result, the memory allocated for that object cannot be reclaimed, eventually leading to an **OutOfMemoryError (OOM)** crash. 6*d9e8da70SAndroid Build Coastguard Worker 7*d9e8da70SAndroid Build Coastguard WorkerFor example, an Android `Activity` instance is no longer needed after its `onDestroy()` method is called, and storing a reference to that instance in a static field prevents it from being garbage collected. 8*d9e8da70SAndroid Build Coastguard Worker 9*d9e8da70SAndroid Build Coastguard Worker## Common causes for memory leaks 10*d9e8da70SAndroid Build Coastguard Worker 11*d9e8da70SAndroid Build Coastguard WorkerMost memory leaks are caused by bugs related to the lifecycle of objects. Here are a few common Android mistakes: 12*d9e8da70SAndroid Build Coastguard Worker 13*d9e8da70SAndroid Build Coastguard Worker* Adding a `Fragment` instance to the backstack without clearing that Fragment's view fields in `Fragment.onDestroyView()` (more details in [this StackOverflow answer](https://stackoverflow.com/a/59504797/703646)). 14*d9e8da70SAndroid Build Coastguard Worker* Storing an `Activity` instance as a `Context` field in an object that survives activity recreation due to configuration changes. 15*d9e8da70SAndroid Build Coastguard Worker* Registering a listener, broadcast receiver or RxJava subscription which references an object with lifecycle, and forgetting to unregister when the lifecycle reaches its end. 16*d9e8da70SAndroid Build Coastguard Worker 17*d9e8da70SAndroid Build Coastguard Worker## Why should I use LeakCanary? 18*d9e8da70SAndroid Build Coastguard Worker 19*d9e8da70SAndroid Build Coastguard WorkerMemory leaks are very common in Android apps and the accumulation of small memory leaks causes apps to run out of memory and crash with an OOM. LeakCanary will help you find and fix these memory leaks during development. When Square engineers first enabled LeakCanary in the Square Point Of Sale app, they were able to fix several leaks and reduced the OOM crash rate by **94%**. 20*d9e8da70SAndroid Build Coastguard Worker 21*d9e8da70SAndroid Build Coastguard Worker!!! info 22*d9e8da70SAndroid Build Coastguard Worker **Your crash reporting tool might not correctly report OOMs**. When memory is low because of memory leak accumulation, an OOM can be thrown from anywhere in the app code, which means that every OOM has a different stacktrace. So instead of one crash entry with a 1000 crashes, OOMs get reported as 1000 distinct crashes and hide in the long tail of low occurring crashes. 23*d9e8da70SAndroid Build Coastguard Worker 24*d9e8da70SAndroid Build Coastguard WorkerWhat's next? Learn [how LeakCanary works](fundamentals-how-leakcanary-works.md)!