Search code examples
androidgarbage-collectionout-of-memoryflags

Android FLAG_NEW_TASK and FLAG_SINGLE_TOP activity not garbage collected


I am having an issue with an application.

I am starting an activity from outside of the context of an activity so I have to flag it with FLAG_NEW_TASK (otherwise I got an exception telling me that I have to add the flag).

The issue I am having is that the activity and its views are not garbage collected after onDestroyed in called, so every time the activity is started, it ask for more memory and after a certain time, it runs into an OutOfMemory exception.

I used MAT in order to analyse the memory, and it tells me there are X instances of the activity in the memory, X being the number of time the activity has been started.

I do not have any static references to the activity or the views it holds

Is there any way to get the activity correctly garbage collected or is there anything I am not aware of regarding the flags I am using ?

Thanks

EDIT 1

The only way I found to work around it is to put the activity in its own process and to kill the process when the activity finishes. But I know this is not the right way at all...


Solution

  • This can happen if references to your activity still exist. There are lots of ways this can happen -- one of the most common is to have an AsyncTask still running.

    Here's an example of an activity leak from a Handler inner class: http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html