Avoid memory leak with WeakReference Android

Nick

I have this code example below and i want to make sure that no memory will happen. By passing the whole activity to an async task will lead to a memory leak if the task itself will have a bigger lifecycle than activity's. But if i declare a weakReference, means that if a rotation is about to happen, the activity will be destroyed without memory leak. Am right guys? Do i have to set something to null also?

Main .java

public class Main extends Activity {

  private TextView mMessageView;

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

    new Background(this /*getBaseContext()*/ ).execute();
  }
}

Background.java

 public class Background extends AsyncTask<String, Void, String>
{
    private WeakReference activity;

    public void Background(Activity act)
    {
          activity = new WeakReference(act);
    }

    @Override
    protected String doInBackground(String... params)
    {

    }

    @Override
    protected void onPostExecute(String result)
    {  
            if(activity.get()!=null)
            //ToDo
    }
}
Andrei N

There is no need for explicit nulling when using WeakReference. Regarding the use of AsyncTask inside an Activity, just be careful not to create an anonymous instance because it will have an implicit reference to the enclosing class which can lead to memory leaks.

The code you posted seems ok, don't forget to terminate the AsyncTask gracefully when the activity is recreated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to release memory in android to avoid memory leak

From Dev

JavaFX WeakReference memory leak when drawing charts

From Dev

Avoid Memory leak

From Dev

How to avoid this memory leak?

From Dev

Android WeakReference to handle memory leakage

From Dev

Weak reference instead of getActivity() (Android avoid memory leak)?

From Dev

Android animations memory leak

From Dev

Is it a memory leak in android

From Dev

memory leak with Android WebView

From Dev

Is it a memory leak in android

From Dev

Android - is this a memory leak?

From Dev

Prevent memory leak in Android

From Dev

nodejs setMaxListeners to avoid memory leak detection

From Dev

Ways to avoid memory leak when exception thrown

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

weak to non class types to avoid memory leak

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How can I fix to avoid a memory leak?

From Dev

How can I avoid a memory leak in this function?

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

Is setting ulimit -v sufficient to avoid memory leak

From Dev

Android inner classes memory leak and leak by context?

From Dev

Android - Device Memory Leak with Fragments

From Dev

Android Fragment Webview Memory Leak

From Dev

Android runOnUiThread causing memory leak

From Dev

Android camera Bitmap memory leak

From Dev

Android progress bar memory leak

From Dev

`Unknown` (`Other`) memory leak in Android?

From Dev

Android runOnUiThread causing memory leak