退出Activity后如何释放实际的Activity对象内存?
我的 Android 应用程序出现 OutofMemoryError,并且对发生的情况感到有点困惑。基本上发生的情况是,我能够在前几次运行它,但是当我尝试退出它然后快速重复地再次打开它时,我收到内存不足错误。
我尝试研究这个主题,发现 recycle() 方法通常是问题所在。但是,我在每个位图上调用了回收方法(存储在对象容器中,存储在数组列表中),但仍然遇到问题。
完成此操作后,我尝试使用 Eclipse 内存分析器来查看堆转储,这时我遇到了一些奇怪的情况。退出活动(返回启动器活动,然后通过按钮再次打开活动)后,我使用内存分析器截取了堆转储的屏幕截图。事实证明,每次我退出并重新进入当 onDestroy() 方法被调用时(其中也有回收/清理代码),正在创建活动对象的另一个实例,并且旧的实例没有被释放。
然后我尝试重写 Finalize 方法看看是不是当活动退出回到启动器活动时被调用,但我在其他一些 stackoverflow 线程上读到 Finalize() 并不总是被调用,所以最后,我仍然不确定。 最终
,我的问题是: 我应该如何确保从活动退出到另一个活动后释放活动对象(活动对象本身,而不是从活动创建的东西)?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您的应用程序存在内存泄漏,我建议您点击以下链接:
避免内存泄漏
Android 应用的内存管理
Sounds like you application suffers from memory leaks, i recommend you to follow the links below:
Avoiding Memory Leaks
Memory management for Android Apps
一种选择是在清单文件中将启动模式设置为 singleInstance 或 singleTask。这将确保不会创建该活动的另一个实例。
文档示例
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
释放 Activity 对象的一种方法是调用它的 onDestoy() 方法。将 Activity 的一个对象设为公共静态并在 onDestroy() 中将其设为 null
然后在 onCreate 方法中通过此方法
和 onDestroy() 方法 初始化 obj这样做:
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()
And then in onCreate method initalize obj by this
and in onDestroy() method do this: