Android的活动垃圾收集 [英] Android Activity Garbage Collection

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

问题描述

我注意到这种行为,而开发一个简单的Andr​​oid游戏有2活动。

游戏中有2活动,首先是一个屏幕,它允许用户选择对手类型,水平等,并且所述第二实际游戏画面。
第二个活动创建负责处理所有的游戏处理的游戏管理类的对象。
这个游戏管理类还创建它的启动,提示用户输入(超时游戏默认为对手)。

一个CountDownTimer

我已经注意到,如果用户退出第二个活动(返回到第一个),然后重新启动一个新的游戏中,previous计时器仍在运行,直到完成。
我已经被作为timerobject只是设置为空并没有取消计时器(第二次活动从的onDestroy())明确取消计时器来处理这一点。

不过我很好奇,为什么previous计时器甚至在我的活动是退出第一次运行?如果不是GC已经删除了第二个活动实例的所有对象(和任何子对象它创造),当它退出?将是巨大的了解背后的观察到的行为的原因是什么?

TIA


解决方案

  

应该不是GC已经删除了第二个活动实例的所有对象(和任何子对象它创造),当它退出?


这是不是垃圾收集是如何工作的。该GC不负责删除对象 - 这是负责捡'孤儿'对象,并释放其资源。即使是这样,一个GC是不能保证得到所有的孤儿及时。

另外到,这可能是系统对象,并需要明确释放任何物体可能永远不会,如果你的code没有做释放。与GC的其他问题可能包括创建它的其他线程(比创建它们的活动除外)可以有一个参考的对象。

您提到您的定时器,但不说明你正在使用什么样的类吧。我建议读了专门有关这个类,看看有什么的影响是ceation /缺失(资源可能是明显的释放)。

GC是在任何平台上一个很灰色地带。有了Android它通常pretty立即但随着活动的生命周期的本质是为predict很难会发生什么。

在内的活动和还搞什么savedInstanceState和共享preferences跟踪到底是怎么回事的一般化妆用的onCreate,和的onPause的onResume

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

解决方案

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.

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

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