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
After QQ and WeChat have dominated the social net...
When it comes to event planning, what everyone th...
Recently, Chengdu Qianfeng Electronics Co., Ltd. ...
"Sony laptops are giving back as a token of ...
Learn to master: Let friends with no basic knowle...
Forums, as one of the first gathering places for ...
How to easily obtain accurate traffic. Maybe you ...
Produced by: Science Popularization China Author:...
The TV series "In the Name of Family" i...
On December 4, archaeological experts from Hubei ...
As a chosen worker, you Are you wondering: Sittin...
Produced by | Science Popularization China Author...
Which “food assassins” are clearly carcinogenic? ...
This course mainly explains how to attract traffi...
What I am disassembling today is not a specific o...