0

People , I have an async task which downloads some news items using a webservice . Here is the psuedo for that .

I have an operation that i perform in an asynctask . It downloads some news from a webservice .

AsyncTask
{

AsyncTask(Context context){}

doInBackground()
{
 // download some news 
 ..
..
 return List<News>
}

onpostExecute(List <News>)
{
 is the activity around ? if yes then
  //find the list using context ,and populate it with news
}

}

My question is that , in midst of operation if the device is oriented , we will have a new AsyncTask . Now the old one already in progress has an internal List .

Would that be garbage collected ? . I think it wont . I smell the concept of weakreferences being applied on this situation , but I can't trust my nose here .

Also by weakreferences , I may know inside onPostExecute that the activity that spawned this Async is no longer around . But . I still believe I am missing a lot .

Or is this nothing I should worry about and the List would automatically be garbage collected once the asynctask finishes ?

Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59

2 Answers2

0

You have a new AsyncTask because the activity is recreated, and you are probably spawning the task in onCreate | onStart | onResume.

You could disable activity restart on config changes, but this is tricky. Just assume your activity might be restarted, and implement it so that this can be done safely.

Since you are passing the context as parameter, I'd assume the task is an static inner class or an external class in its own file.

You could try to use the "application" context instead of the "activity" context, as advised in this blog post: http://android-developers.blogspot.com.es/2009/01/avoiding-memory-leaks.html

The other option is, as you said, have a WeakReference to the context.

But other than this, the important thing here is whether those two tasks can safely run simultaneously. Also running a task to its completion when you know the result isn't going to be consumed by the visible activity is a waste of resources. I'd cancel the current task when the activity is shutting down (onStop | onPause | onDestroy), but this is also tricky (you need to implement it with the intent of cancelling it ASAP the thread is interrupted. More info here: Android - Cancel AsyncTask Forcefully).

The most robust way to go is to use a Service instead of the AsyncTask.

Community
  • 1
  • 1
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • thanks for the reply. However as you can see my question was more related to the behaviour of the garbage collector . I mean I dont have any issue currently , however i STRONGLY forsee that I may have one in future , as I imply in my question . – Muhammad Ahmed AbuTalib Nov 11 '13 at 11:03
  • I think the more expensive object in terms of memory is the activity context. The list, you'd have declared it in the asynctask class, so it would be collected also IF the task is collected. – Mister Smith Nov 11 '13 at 11:17
0

There is the thread Is AsyncTask really conceptually flawed or am I just missing something?

Briefly, an Activity is a Controller from the MVC (Model-View-Controller) viewpoint, and your data belong to Model. So just do not invoke such AsyncTasks directly from the Activity.

One more writing: AsyncTask Missteps http://www.shanekirk.com/2012/04/asynctask-missteps/

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127