Original text: LeakCanary: Detect all memory leaks!
Nobody likes OutOfMemoryError During Square's sign-up process, we draw a user's signature on a bitmap. This bitmap is the same size as the device's screen, and when creating it, I encountered a considerable number of OOM-related crashes. We have tried several approaches, none of which solved our problem:
The way we look at the problem is wrong The size of the bitmap itself is not a problem. When the memory is almost full, OOM can happen at any time. It is especially likely to happen when creating large objects, such as bitmaps. OOM usually represents a deeper problem: memory leak. What is a memory leak? Some objects have a limited lifespan. When their job is done, they are garbage collected. If a series of references to an object are still being held when its lifespan should have ended, this can lead to a memory leak. As leaks accumulate, your app will run out of memory. For example, after Activity.onDestroy() is called, the view tree and the associated bitmap should be garbage collected. If a running background thread continues to hold a reference to the Activity, the associated memory will not be reclaimed, which will eventually lead to an OutOfMemoryError crash. Finding memory leaks Finding memory leaks is a manual process that is well described in the Wrangling Dalvik series by Raizlabs. Here are the key steps:
It would be nice if there was a library that could do all that for you, allowing you to focus on fixing memory leaks. Introduction to LeakCanary LeakCanary is an open source Java library for detecting memory leaks in debug versions. Let's look at an example of cait:
Create a RefWatcher instance and give it an object to watch:
When a leak is detected, you automatically get a nice leak clue:
We know your time is valuable, so we made it very easy to set up. With just a few lines of code, LeakCanary can automatically detect leaks in your Activity:
When memory is low, there will be a notification and a nice display interface: in conclusion After enabling LeakCanary, we found and fixed many memory leaks. We even found some leaks in the Android SDK. The results are pretty amazing, we now have a 94% reduction in OOM crashes. If you want to put an end to OOM crashes, install LeakCanary now! |
<<: JSPatch – Dynamically update iOS apps
>>: Cocos game development engine adds efficient wings to HTML5 game development
[[125942]] Although Nokia's voice in the cons...
Account optimization refers to the process of con...
Although there is still a month to go until June ...
Recently, Huawei and Apple have launched satellit...
In the early morning of July 27, I saw several on...
This article shares with you the latest "Bai...
I have written articles about free gifts on Xiany...
Imagine that when we get off work and walk out of...
Why is Tik Tok so magical in promoting products? ...
For a prestigious automobile company, falling beh...
[Introduction] DAU, GMV, CPM, CPC... This series ...
Baidu Experience is a product of Baidu and has a ...
The global enterprise wireless LAN (WLAN) market ...
This June, many areas in northern my country seem...