Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if we have memory leak in Android studio

Tags:

android

Recently I tried Android Studio It seems my app is not working and after a while stops working! Here is the profiler screenshot

Image

My question is, How to detect the memory leak? As I press the dump head memory app stops from running.

Are the sharp edges represents the memory leak?

like image 483
Ehsan Aslmostafa Avatar asked Nov 29 '18 05:11

Ehsan Aslmostafa


2 Answers

Are the sharp edges represnts the memory leak?

No. It's the other way around. The sharp edges you see result from the garbage collector reclaiming memory from your app. With a memory leak, your app's memory usage would increase over time.

However, it seems your app is creating lots of objects. This is normal as long as your app actually does something useful (i.e. not just sitting there waiting for user input).

like image 116
Axel Avatar answered Oct 17 '22 03:10

Axel


There are different methods for checking Memory Leak in Android

One of most famous is LeakCanary by square

A memory leak detection library for Android and Java.

“A small leak will sink a great ship.” - Benjamin Franklin

In your build.gradle:

dependencies {
  debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
  releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
}

In your 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...
  }
}

In your Manifest.xml

<application
    android:name=".ExampleApplication"
    ...
 >
 
like image 2
Ali Ahmed Avatar answered Oct 17 '22 02:10

Ali Ahmed