<i id='2wbCp'><tr id='2wbCp'><dt id='2wbCp'><q id='2wbCp'><span id='2wbCp'><b id='2wbCp'><form id='2wbCp'><ins id='2wbCp'></ins><ul id='2wbCp'></ul><sub id='2wbCp'></sub></form><legend id='2wbCp'></legend><bdo id='2wbCp'><pre id='2wbCp'><center id='2wbCp'></center></pre></bdo></b><th id='2wbCp'></th></span></q></dt></tr></i><div id='2wbCp'><tfoot id='2wbCp'></tfoot><dl id='2wbCp'><fieldset id='2wbCp'></fieldset></dl></div>
    <bdo id='2wbCp'></bdo><ul id='2wbCp'></ul>

    <small id='2wbCp'></small><noframes id='2wbCp'>

    <legend id='2wbCp'><style id='2wbCp'><dir id='2wbCp'><q id='2wbCp'></q></dir></style></legend>

      <tfoot id='2wbCp'></tfoot>
    1. Android Activity 垃圾收集

      时间:2023-05-19
      <legend id='G7zYy'><style id='G7zYy'><dir id='G7zYy'><q id='G7zYy'></q></dir></style></legend>
        <tbody id='G7zYy'></tbody>
      <tfoot id='G7zYy'></tfoot>

        <i id='G7zYy'><tr id='G7zYy'><dt id='G7zYy'><q id='G7zYy'><span id='G7zYy'><b id='G7zYy'><form id='G7zYy'><ins id='G7zYy'></ins><ul id='G7zYy'></ul><sub id='G7zYy'></sub></form><legend id='G7zYy'></legend><bdo id='G7zYy'><pre id='G7zYy'><center id='G7zYy'></center></pre></bdo></b><th id='G7zYy'></th></span></q></dt></tr></i><div id='G7zYy'><tfoot id='G7zYy'></tfoot><dl id='G7zYy'><fieldset id='G7zYy'></fieldset></dl></div>
          • <small id='G7zYy'></small><noframes id='G7zYy'>

              • <bdo id='G7zYy'></bdo><ul id='G7zYy'></ul>
              • 本文介绍了Android Activity 垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我在开发一个包含 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 垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Android 中的垃圾收集器正在运行,但 ddms 的分配跟踪器中没有显示任何已分配的内容 下一篇:在 Firemonkey 中滚动后,ListBox 项目发生变化

                相关文章

              • <small id='pswgo'></small><noframes id='pswgo'>

                <tfoot id='pswgo'></tfoot>

                <legend id='pswgo'><style id='pswgo'><dir id='pswgo'><q id='pswgo'></q></dir></style></legend>
                <i id='pswgo'><tr id='pswgo'><dt id='pswgo'><q id='pswgo'><span id='pswgo'><b id='pswgo'><form id='pswgo'><ins id='pswgo'></ins><ul id='pswgo'></ul><sub id='pswgo'></sub></form><legend id='pswgo'></legend><bdo id='pswgo'><pre id='pswgo'><center id='pswgo'></center></pre></bdo></b><th id='pswgo'></th></span></q></dt></tr></i><div id='pswgo'><tfoot id='pswgo'></tfoot><dl id='pswgo'><fieldset id='pswgo'></fieldset></dl></div>
                    <bdo id='pswgo'></bdo><ul id='pswgo'></ul>