12/08/2018, 16:22

Leak Canary for Android

In our pursuit of building robust and better apps, we as developers need to take into consideration many things in order to be professional ands stay on track, one of which is to make sure that our apps does not crash. A common cause of crashes are memory leaks. This particular problem can manifest ...

In our pursuit of building robust and better apps, we as developers need to take into consideration many things in order to be professional ands stay on track, one of which is to make sure that our apps does not crash. A common cause of crashes are memory leaks. This particular problem can manifest itself in various forms. In most cases there is a steady/huge increase in memory usage until the app cannot allocate any more resources and by doing so inevitably crashes. In Java this often results in an OutOfMemoryException being thrown. In some rare cases, leaked classes can even stick around for long enough to receive registered callbacks, causing some really strange bugs and all too often throw the notorious IllegalStateException. In short Memory leaks simple means when code are written in a bad manner that unused objects are referenced somehow from reachable objects, Garbage Collections would mark these unused objects as useful objects and therefore would not be able to remove them.

The Garbage Collection

Note: Every forgotten reference in the memory will create memory leaks therefore it is necessary that these object should be discarded.

The Garbage Collections is activated when memory runs short. Android runtime is responsible for triggering the Garbage Collection when memory runs short. The purpose of Garbage C is to reclaim memory by cleaning up objects that are no longer useful as shown in the image above.

When developing your applications one must take note and avoid memory leaks. Memory leaks can result in unstable app and even crash down of the app. There are many ways memory leaks can happen, it could be from maybe registering a receiver and foegeting to unregister it hereby leading to a leak occurence. Without calling the unregister method, the instance will probably keep a reference around long after the referenced object has been terminated and will thus start leaking memory. In Android this is especially troublesome if that object is an activity, since they often hold a large amount of data. Rather than using the inbuilt android monitor (Monitor Tab) to monitor the memory allocations, Leak canary makes it way easier and without creatting distractions or diverse when creating the project.

Implementation

Leak canary can be added to any application. You do not need to do nothing speacial, just follow the steps below to set it up and running on your application.

  1. Add the library to your gradle file
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
  1. Add to Application class
public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
      // This process is dedicated to LeakCanary for heap analysis.
      // You should not init your app in this process.
      return;
    }
    LeakCanary.install(this);
    // Normal app init code...
  }
}

Thats it! Whenever a memory leak is detected u get a notification on your device and the details of this leak will be displayed to you. Its so easy and so you can easily retify this leaks in the class itslef. Chckout Image below

0