Android Activity 垃圾收集 [英] Android Activity Garbage Collection

查看:23
本文介绍了Android Activity 垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个包含 2 个活动的简单 Android 游戏时注意到了这种行为.

I noticed this behavior while developing a simple Android game which has 2 activities.

游戏有两个活动,第一个是允许用户选择对手类型、级别等的屏幕,第二个是实际游戏屏幕.第二个活动创建一个处理所有游戏处理的 GameManager 类的对象.这个 GameManager 类还创建了一个 CountDownTimer,它开始提示用户输入(超时时游戏默认为对手).

The game has 2 activities, the first is a screen which allows the user to select the opponent type, level etc. and the second the actual game screen. The second activity creates an object of a GameManager class which handles all the game processing. This GameManager class also creates a CountDownTimer which it starts to prompt user input (on timeout the game is defaulted to the opponent).

我注意到,如果用户退出第二个活动(返回到第一个活动)然后再次启动新游戏,则前一个计时器仍在运行直到完成.我已经通过显式取消计时器(从第二个活动的 onDestroy() )来处理这个问题,因为只是将 timerobject 设置为null"并没有取消计时器.

I've noticed that if the user exits the second activity (returns to the first) and then launches a new game again, the previous timer is still running until completion. I've handled this by explicitly cancelling the timer (from the onDestroy() of the second activity) as just setting the timerobject to 'null' did not cancel the timer.

但是我很好奇为什么即使在我的活动第一次退出后前一个计时器仍在运行?GC 不应该在退出时删除第二个 Activity 实例化的所有对象(以及它创建的任何子对象)吗?很高兴知道观察到的行为背后的原因?

However I'm curious as to why the previous timer was running even after my activity was exited the first time? Shouldn't the GC have deleted all the objects instantiated by the second Activity (and whatever child objects it created) when it was exited? Would be great to know the reason behind the observed behavior?

TIA

推荐答案

GC不应该在退出时删除第二个Activity实例化的所有对象(以及它创建的任何子对象)吗?

Shouldn't the GC have deleted all the objects instantiated by the second Activity (and whatever child objects it created) when it was exited?

这不是垃圾收集的工作方式.GC 不负责删除对象"——它负责拾取孤立"对象并释放它们的资源.即使这样,GC 也不能保证及时到达所有孤儿.

This isn't how Garbage Collection works. The GC isn't responsible for 'deleting objects' - it's responsible for picking up 'orphaned' objects and freeing their resources. Even then, a GC isn't guaranteed to get to all of the orphans in a timely manner.

此外,如果您的代码不这样做,任何可能是系统"对象并且需要显式释放的对象都可能永远不会被释放.GC 的其他问题可能包括创建其他线程(创建它们的 Activity 除外)可能引用的对象.

Further to that, any objects which may be 'system' objects and need to be released explicitly may never be released if your code doesn't do it. Other issues with GC may include creating objects which other threads (other than the Activity which created them) may have a reference to.

您提到了您的计时器",但没有解释您使用的是哪种类.我建议专门阅读有关该类的内容,看看对 ceation/deletion 有什么影响(可能是明确的释放"资源).

You mention your 'timer' but don't explain what sort of class you are using for it. I suggest read up specifically about that class and see what the implications are for ceation/deletion (possibly explicit 'release' of resources).

GC 在任何平台上都是一个非常灰色的区域.对于 Android,它通常非常直接,但由于 Activity 生命周期的性质,很难预测会发生什么.

GC is a very grey area on any platform. With Android it's normally pretty immediate but with the nature of the Activity life-cycle it's very difficult to predict what will happen.

通常在活动中使用 onCreate、onPause 和 onResume 以及诸如 savedInstanceState 和 SharedPreferences 之类的东西来跟踪正在发生的事情.

In general make use of onCreate, onPause and onResume within Activities and also things like savedInstanceState and SharedPreferences to keep track of what is going on.

这篇关于Android Activity 垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆