开发者

How to track down memory leak - Find living references

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 r开发者_开发百科eferences 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜