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
Laptop graphics cards have always been weak, and e...
[[284872]] Android Q (Android 10) has been releas...
The impact of the COVID-19 pandemic in 2020 has p...
At 12:31 Beijing time, China Eastern Airlines fli...
The ratings of CCTV's 2015 Spring Festival Ga...
Looking around the world, the hottest and most cu...
2018 was the year when mobile Internet fell from ...
Mixed Knowledge Specially designed to cure confus...
Jazz dance, also known as American modern dance, ...
Switzerland is a picturesque country with highly ...
Advertising has been evolving, have your marketin...
▌The theme of this popular science creation comes...
In recent years, as the number of cars in China c...
Recently, with the temperature fluctuating, many ...
If I have to choose a "ceiling of human tea ...