In 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.
For 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.
LeakCanary’s knowledge of the internals of the Android Framework gives it a unique ability to narrow down the cause of each leak, helping developers dramatically reduce OutOfMemoryError crashes.
Common causes for memory leaks
Most memory leaks are caused by bugs related to the lifecycle of objects. Here are a few common Android mistakes:
- Adding a
Fragmentinstance to the backstack without clearing that Fragment’s view fields in
Fragment.onDestroyView()(more details in this StackOverflow answer).
- Storing an
Activityinstance as a
Contextfield in an object that survives activity recreation due to configuration changes.
- Registering a listener, broadcast receiver or RxJava subscription which references an object with lifecycle, and forgetting to unregister when the lifecycle reaches its end.
Why should I use LeakCanary?
Memory 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%.
How LeakCanary works
Once LeakCanary is installed, it automatically detects and report memory leaks, in 4 steps:
- Detecting retained objects.
- Dumping the heap.
- Analyzing the heap.
- Categorizing leaks.
Mobile App Development Best Practices – 03.10
iOS MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming How to build a Tuist plugin and publish it using...
How to make and use BOM (Bill of Materials) dependencies in Android projects
By using a BOM dependency, you can avoid specifying the versions of each individual library in your app, and let...
Telegram turns 10 years old and revenues stagnate
Telegram seems to want to grow not only through messaging but also through communities, which pretty much means it wants...
MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming
Supercharge Swift‘s Codable implementations with macros. Overview MetaCodable framework exposes custom macros which can be used to generate dynamic Codable implementations. The core of the framework...
How to get started with Swift Concurrency 🧵 (Beginner Tutorial)
Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be suspended and resumed later,...
Mobile App Development Best Practices – 02.10
Data.ai has summarized the interim results of the year – and once again we have a record. Annual consumer spending...