Question

I have a fairly complex app -

The UI Activity launches a Service and sets up 2 way AIDL callbacks so the two can communicate.

The service runs forever (unless hits 'off' button in UI Activity).

The problem is when the user exits the Activity, the Activity memory is not released. -The amount of memory used by my app is the same even if the UI Activity has been closed. -The heap still shows mapTiles and other UI crap on it even if the UI Activity has been closed.

So my guess is somehow the Service is holding references to the Activity. I know the many articles warning about leaking the Activity Context. The Service only references the Activity through a Weak Reference stored at Application scope. (a class that extends Application)

Is there any way to find what specifically is referencing the Activity? The dominator tree shows mapTiles and ListView layouts eating all my memory... but I can't find the reference to the Activity which is keeping that stuff alive.

Also, is there a way to dump an HPROF heap dump thing if OutOfMemoryException occurs?

Was it helpful?

Solution

The problem is when the user exits the Activity, the Activity memory is not released.

There is no "exit Activity" in Android. Calling finish() or pressing back on the activity does not remove objects from memory that the activity used. Android inherently takes care of this itself at some random point in the future.

If you want your objects to be released from memory at the moment the activity finishes, you need to manually release them yourself.

Edit: The class that can be used to find memory usage is the ActivityManager.

OTHER TIPS

If it's not yourself who is holding a reference ("references the Activity through a Weak Reference stored at Application scope", do you null it? I wonder why you reference the activity in the service, usually its the other way), did you try to execute a GC? As long as there is enough memory left, no gc will take place to move the trash away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top