Android 活动垃圾收集

发布于 2024-11-09 00:45:38 字数 448 浏览 0 评论 0原文

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

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

我注意到,如果用户退出第二个活动(返回到第一个活动),然后再次启动新游戏,则前一个计时器仍会运行直到完成。 我通过显式取消计时器(从第二个活动的 onDestroy() )来处理此问题,因为仅将计时器对象设置为“null”并没有取消计时器。

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

TIA

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

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).

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-11-16 00:45:38

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

这不是垃圾收集的工作原理。 GC 不负责“删除对象”——它负责拾取“孤立”对象并释放其资源。即使这样,GC 也不能保证及时到达所有孤儿。

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

您提到了“计时器”,但没有解释您使用的是什么类型的课程。我建议专门阅读该类,看看其对删除/删除的影响(可能是明确的资源“释放”)。

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

一般来说,在 Activity 中使用 onCreate、onPause 和 onResume 以及 savingInstanceState 和 SharedPreferences 等来跟踪正在发生的情况。

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

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.

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.

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 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.

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.

风蛊 2024-11-16 00:45:38

正如您已经发现的那样,CountDownTimer 并未绑定到活动。在这些情况下需要注意的一个提示是类在其构造函数中不会接收任何 Context。因此它不能绑定到活动。

CountDownTimer is not bound to an activity as you already found out. A hint to lookout for in these cases is that a class does not receive any Context in its constructor. Hence it can't be bound to an activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文