安卓:有没有设置引用为null任何优势当活动被销毁? [英] Android: Is there any advantage of settings references to null when an Activity is being destroyed?

查看:165
本文介绍了安卓:有没有设置引用为null任何优势当活动被销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于垃圾收集器使用一个标记 - 清除算法:


  

实际GC使用的是马克 - 扫描算法来实现。这个做完了
  利用位向量来跟踪哪些对象是最初到达
  标记根对象作为到达。然后,GC不会在扫
  堆中对象的地址顺序标记。对于每一个到达
  反对它扫过它从该对象可标记所有对象
  可到达。如果新对象具有较低的地址被加入到
  工作队列。最初的扫描后,工作队列


因此​​,将由有根的引用和所有其他将被收集在那里把一个参考空当上的活动是有用的任何情况下每一个对象传递?

的onDestroy 把我认为没有任何优势是否正确?
怎么样把他们作为空的的onPause 的重的活动(验证 isFinishing 之后)。这将有助于更快地得到收集的资源呢?


解决方案

  

因此​​,将由具有根和所有其他将被收集一提的是有把一个引用空一项活动时,任何情况下有用的每一个对象传递?


是的。如果它的静态


  

把上的onDestroy我认为没有任何优势是否正确?


我会说这是可以忽略不计,因此无意义的,因为活动将是GC-根少本身暂时


  

...怎么样把他们作为空上的onPause的重的活动。这将有助于更快地得到收集到的资源?


如果一个对象不具有GC根则适合于GC。 的onPause 在某一点发生之前的onDestroy (取决于执行的情况下,这可能是的任何的时间范围)ERGO的的上述问题。

但是!您不想这样做,因为你的活动,经的onPause 回来前景和你将不得不分配所有你的'重'了对象 - 这将最有可能为最终用户一个蹩脚的体验结束了

Since the garbage collector uses a Mark-Sweep algorithm:

The actual GC is done using a Mark-Sweep algorithm. This is done using a bitvector to track which objects are reachable initially marking root objects as reachable. Then the GC does a sweep over the objects in the heap in address order marking. For each reachable object it sweeps over it marks all objects reachable from that object as reachable. If the new object has a lower address it is added to a work queue. After the initial sweep the work queue is

So it will pass by every object that has a reference to the root and every other will be collected is there any case when putting a reference to null on an Activity is useful?

Putting on onDestroy I assume that has no advantage correct? What about putting them as null on onPause (after validating isFinishing) on "heavy" activities. Will it help the resources to get collected sooner?

解决方案

So it will pass by every object that has a reference to the root and every other will be collected is there any case when putting a reference to null on an Activity is useful?

Yes. If its static

Putting on onDestroy I assume that has no advantage correct?

I would say this is negligible and therefore pointless as the Activity will be GC-root-less itself momentarily

...What about putting them as null on onPause on "heavy" activities. Will it help the resources to get collected sooner?

If an object does not have a GC root it is eligible for GC. OnPause happens at some point before onDestroy (depending on the execution case this may be any range of time) ergo yes to the above question.

BUT! You would not want to do this as your activity may come back to the foreground after onPause and your would have to allocate all your 'heavy' objects again - which would most likely end up in a crappy experience for the end-user.

这篇关于安卓:有没有设置引用为null任何优势当活动被销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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