Android app memory leak identification and fixing

Recently, I worked on android app memory optimization. During memory optimization I used some tools and techniques, which help Android developers to identify memory leaks quickly and easily, also it help in finding out code which causes memory leaks. So let’s start!

Memory Leak Identification

As a first step I added leakcanary library provided by square in app, which gives you notification about memory leaks as you run app and traverse through app . You can get more information about this library here. Other tool which I used for memory leak identification is hprof view , memory monitor of Android studio 2.2.3.

Let’s first talk about leakcanary . When you install your app after integrating leakcanary , you will see additional app called “Leaks”. When you click on Leaks app it will show list of leaks and when you click on list item it will show reference trace due which leak has occurred. You can also see reference trace in the logcat whenever leakcanary identify leak in your app. Looking at reference trace you can figure out code, which is responsible for that particular memory leak. One important thing is that it works in debug builds.

Now let’s talks about how we can identify memory leaks in app using hprof view , memory monitor of Android studio 2.2.3. First connect your device to your laptop, then install and run debug build using Android Studio on your device and then open memory monitor as shown in below figure and take heap dump by clicking on heap dump icon in memory monitor as indicated by red arrow in below figure, while navigating from one activity to another activity.

When heap dump process completed Android Studio open up hprof view as shown in below figure. To know about memory leaks you need open “Analyser Tasks” by clicking on “Analyser Tasks ” as shown by red arrow in below figure.

Once “Analyser Tasks” visible to you, you need to check “Detect Leaked Activities” and run “Analyser Tasks” by clicking on green arrow as shown by red arrow in below figure.

When “Analyser Tasks” completed it will show leaked activities in “Analysis Results” section as shown by red arrow. When you click on leaked activity it will show reference trace in “Reference Tree” section, due to which that particular activity get leaked.

Memory Leak Fixing

We talked enough about memory leak detection or identification. Now let’s talk about memory leak fixing.

When I investigated my app for memory leaks, I found some common causes due to which memory leak was happening. For other app there might be some additional causes due to which memory leaks exist in those app.

Network callback as a anonymous inner class : This was major root cause in my app for for most of memory leaks. When i investigated I found that object of anonymous class holds reference of container class and in my case this was an activity. So when I navigate away from activity object of network callback hold reference of that activity and due to which memory leak occurs. I removed these kind of memory leaks from my app by declaring network callbacks as static inner classes and holding activity reference as a weak reference inside static inner class.

Webview: I was using webview in my app for showing ticker. I declared webview in layout file of my activity. During my investigation I found that this webview was leaking container activity. So, I removed this webview from layout file and added in linearlayout in onResume() method of activity as shown in below code snippet:

tickerWebView = new WebView(getApplicationContext());
tickerLayout = (LinearLayout)findViewById(R.id.ticker_layout);
tickerLayout.addView(tickerWebView);

I also removed webview in onStop() method of activity as shown in below code snippet:

tickerWebView.removeAllViewsInLayout();
tickerWebView.destroy();
tickerLayout.removeAllViews();
tickerWebView = null;

Activity Context: We should not pass activity context to other methods of our app until unless it is necessary to pass. In some of the cases it was also the reason of memory leak in my app.

Static Reference of Activity: We should not use static reference for an activity. if we use static reference then there will be always reference for activity due which activity can not be garbage collected and activity will be leaked.


If you like this article please recommend and share this article on facebook and twitter. Your thoughts and suggestions also welcome.