开发者

How to release the actual Activity object memory after quitting out of the activity?

开发者 https://www.devze.com 2023-03-23 00:14 出处:网络
I\'m getting an OutofMemoryError for my android application, and am a bit confused as to whats going on. Basically what happens is, I\'m able to run it the first few times, but when I try to quit out

I'm getting an OutofMemoryError for my android application, and am a bit confused as to whats going on. Basically what happens is, I'm able to run it the first few times, but when I try to quit out of it and then open it again quickly and repetitively, I get an out of memory error.

I've tried researching this topic, and have found that the recycle() method has commonly been the problem. However, I've called the recycle method on each of the bitmaps (which are stored in an object container, stored in an arraylist), but was still getting the problem.

After doing this, I tried using the Eclipse Memory Analyzer to look at heap dumps, when I came across something weird. After quitting out of the activity (back into the launcher activity, and then opening up the activity again via a button, I took screenshots of the heap dump using the memory analyzer. It turns out, with each time I quit and re-entered the activity, another instance of the activity object was being created, and the old ones weren't being released, even though the onDestroy() method was being called (which also had the recycle/cleanup code.

I then tried overriding the finalize method to see if it was being开发者_如何转开发 called when the activity quit out back into the launcher activity, but it wasn't being called. I read on some other stackoverflow threads that finalize() isn't always called, so in the end, I'm still not sure whats going on.

Ultimately, my question is this: How am I supposed to ensure that the Activity object (the activity object itself, not the stuff created from the activity) is released after quitting out from the activity into another activity?


Sounds like you application suffers from memory leaks, i recommend you to follow the links below:
Avoiding Memory Leaks

Memory management for Android Apps


One option is setting the launch mode to singleInstance or singleTask in your manifest file. This would make sure that another instance of the Activity is not created.

Documentation Example


One way to release your object of activity is calling it onDestoy() method..Take an object of your activity make it public static and make it null in onDestroy()

Public static Your_activity obj;

And then in onCreate method initalize obj by this

obj=this;

and in onDestroy() method do this:

obj= null;
0

精彩评论

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