Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check and Avoid Memory Leaks in Application

So, I am done with a project, now the main issue I am facing is the Memory Leakage in the application (“leak” meaning you keep a reference to an activity, thus preventing the GC from collecting it)

Some of the cases I found, where the memory leakage occurs are:

Context Leakage

It occurs because of long lived reference to the activity context.

A very good example of it i found here,

private static Drawable sBackground;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);

  setContentView(label);
}

here the problem is with private static Drawable sBackground; The static Drawable is created with the Activity as the context, so in THAT case, there's a static reference to a Drawable that references the Activity, and that's why there's a leak. As long as that reference exists, the Activity will be kept in memory, leaking all of its views.

Screen orientation change

The second case that draws the attention is when the screen orientation changes. When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the application’s UI from the resources. Now imagine you wrote an application with a large bitmap that you don’t want to load on every rotation.

This will result in a lot of memory leakage as there could be large bitmaps to load.

context-activity

The third case, I found was the reference to the activity context. It also results in memory leakage.

I wonder if there is any easy way to avoid such memory leakages from happening. or if there could be tool to check and remove those memory leakages from the application.

like image 556
Sahil Mahajan Mj Avatar asked Dec 04 '12 05:12

Sahil Mahajan Mj


People also ask

How do I check for memory leaks on my application?

To find a memory leak, you've got to look at the system's RAM usage. This can be accomplished in Windows by using the Resource Monitor. In Windows 11/10/8.1: Press Windows+R to open the Run dialog; enter "resmon" and click OK.

How can we avoid memory leak?

To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

What is a memory leak in an application?

Memory leaks are a class of bugs where the application fails to release memory when no longer needed. Over time, memory leaks affect the performance of both the particular application as well as the operating system. A large leak might result in unacceptable response times due to excessive paging.


2 Answers

I know this is an old post, but recently Square released a library called LeakCanary which is by far the most elegant solution for finding memory leaks.

like image 185
Caleb_Allen Avatar answered Sep 21 '22 14:09

Caleb_Allen


you can use tools like traceview or memory analyzer to check for memory leaks

http://developer.android.com/tools/help/traceview.html

http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html

Here are some articles i found helpful

http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

http://vahidmlj.blogspot.com/2012/10/android-memory-leak-on-screen-rotation.html

like image 23
stinepike Avatar answered Sep 24 '22 14:09

stinepike