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
Among the many sea turtles, the Hawksbill is undo...
As the fourth LeEco sub-business to complete inde...
NBA basketball teaching_professional basketball t...
A landing page, also known as a landing page or a...
I left my computer, walked into the bathroom, and...
Heilongjiang: The technological power behind the ...
As a programmer, I never worked for the same comp...
Review expert: Cao Qingwei, deputy chief physicia...
Today (May 4th) is the first day of rework on May...
"Forrest Gump" said: "Life is like...
Source: Dr. Curious The cover image and the image...
In recent years, content marketing has become a h...
macaca provides a command to start the webdriver ...
Recently, the "Knowledge is Power" scie...
Preface User operation is the most important part...