Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : References to a Context and memory leaks

I've read that it is a mistake and a source of memory leaks in Android application to keep a long-lived references to a Context.

But I don't understand if it is ok to create a class that looks like this one:

public class HelperClass {     private Context context;      public HelperClass(Context context) {         this.context = context;     }     public void myHelperMethod() {         // uses this.context     } } 

And call it from an Activity:

public class MyActivity extends Activity {     public void onCreate(Bundle savedInstanceState) {         HelperClass h = new HelperClass(this);         h.myHelperMethod();     }      ... } 
like image 276
Guido Avatar asked Jul 27 '10 17:07

Guido


People also ask

What is context leak in Android?

This is the Context of the process where activities run and it is used in classes that exceed the lifespan of the Activity, such as background tasks or data access.

How does memory leak happen in Android?

Memory leaks occur when an application allocates memory for an object, but then fails to release the memory when the object is no longer being used. Over time, leaked memory accumulates and results in poor app performance and even crashes.

How do you find memory leaks in Android application?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.

What are some best practices to avoid memory leaks on Android?

Causes of Memory Leaks and Their SolutionsOne should not use static views while developing the application, as static views are never destroyed. One should never use the Context as static, because that context will be available through the life of the application, and will not be restricted to the particular activity.


2 Answers

This is fine, and will not cause a memory leak.

As soon as onCreate finishes executing, h will be out of scope and become eligible for garbage collection. If h was static, then you would run into problems. Only when the reference to the context outlives the lifecycle of the context itself will a memory leak occur. A few helpful hints:

  • Use Context.getApplicationContext() when possible. This context will live as long as your application is alive.
  • Be careful when using static fields and inner classes.
  • Run your application through a profiler to check for leaks.
like image 86
dbyrne Avatar answered Oct 20 '22 18:10

dbyrne


The scope of the HelperClass is only within your onCreate function, so once onCreate executed, your "h" object is no longer needed and subject to garbage collection.

It would be a different story if "h" was a static member - THAT would be a great way to leak memory.

like image 35
EboMike Avatar answered Oct 20 '22 18:10

EboMike