Android--leakcanary

Source: Internet
Author: User

Https://github.com/square/leakcanary

We tried some methods, but none of them solved.
For example:

1. Use bitmap's Bitmap.Config.ALPHA_82. Capture an Oom exception, then go to GC to reclaim it, and then recreate 3. We didn't expect to collect bitmap Java heap memory. 2. We have seen it in the wrong way.
问题并不在bitmap的大小上,当内存快满的时候,肯定会抛出OOM的,当你试图创建大的对象时,会经常抛出OOM异常。而出现OOM更深层次的问题是在于内存泄露。
3. What is a memory leak?
一些对象有自己限制的生命周期,当他们的工作结束时,他们试图去被gc回收,如果当生命周期结束以后内存中仍然握着这个对象的一些应用的时候,就会出现内存泄露。当内存泄露越来越多的时候,就会出现OOM。例如, 当Activity.onDestroy()被调用的时候,activity对于的布局,相关的bitmap对象都应该被gc回收,如果一个后台线程在运行,并且握着activity的一个引用,那这个引用就没法背回收,最后就会导致OOM。
4. Listen for memory leaks.

Listening for memory leaks is a manual process.
Here are a few key steps:

1. Learn the relevant knowledge about Oom. 2. Try to reproduce the problem. You need to find the phone that has the problem, and you need to figure out where the problem is. 3. Empty heap memory when an oom occurs. 4. Use Mat or other tools to find which objects need to be recycled. 5. Calculate the shortest path from the object to the root of the GC. (What is path from that object to the GC roots) 6. Identify any references that should not exist under this path and fix the memory leak. 5. Introduction Leakcanaryleakcanary is an open-source Java class library that detects memory leaks in the development version.

Let's see an example in Cat:

class Cat {}class Box {  Cat hiddenCat;}class Docker {  static Box container;}// ...Box box = new Box();Cat schrodingerCat = new Cat();box.hiddenCat = schrodingerCat;Docker.container = box;

You need to create a Refwatcher instance and give him an object to observe.

// We expect schrodingerCat to be gone soon (or not), let‘s watch it.refWatcher.watch(schrodingerCat);

When memory is detected, you automatically get a leaked stack of information.

static Docker.container* references Box.hiddenCat* leaks Cat instance

We know that programmers are very busy, so we made a and easy way to create, just one line of code, Leakcanary will automatically detect the activity of memory leaks.

 public < Span class= "Hljs-keyword" >class exampleapplication extends Span class= "Hljs-title" >application { @Override public Span class= "Hljs-keyword" >void oncreate () {super.onCreate (); Leakcanary.install (this);}          

You'll get a reminder, and a nice display interface

6. Conclusion
启用 LeakCanary以后,我们发现并修正了在我们的应用程序的许多内存泄漏。我们甚至发现在Android SDK中的几个漏洞。现在,我们已经从OOM错误中减少94%的Crash了。

Android--leakcanary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.