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
It's autumn now and the weather is getting co...
[[124508]] The American technology blog Re/code w...
“It is as important to have a teacher as it is to...
According to the official information from the Ch...
Randy Shoup, who helped lead engineering teams at...
If the global chip shortage continues, spreading ...
Xiaomi mobile phones became popular due to low pr...
A complete trading system worth eleven years 18 Do...
On April 12, 1961, Soviet astronaut Gagarin took ...
Audit expert: Wang Xuejiang Professor of Pathophy...
In the Internet era, product competition is fierc...
My understanding of user- attracting products inc...
On October 30, as the "ignition" comman...
In recent years, the popularity of "short vi...
Produced by: Science Popularization China Author:...