的onDestroy() - 设置或不设置实例变量为空? [英] onDestroy() - to set or not to set instance variables to null?

查看:280
本文介绍了的onDestroy() - 设置或不设置实例变量为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个好主意,专门设置的实例变量为NULL的onDestroy()活动的回调?事情是这样的:

Is it a good idea to specifically set the instance variables to null in onDestroy() callback of the activity? Something like this:

    @Override
protected void onDestroy() {
    super.onDestroy();
    mClassVariable1 = null;
    mClassVariable2 = null;
    mClassVariable3 = null;
}

如果我没有记错从Java SE,是分离的放大器的任何引用;没有连接到正在运行的程序和放大器;可以被垃圾收集反正。那么,这是否作出上述多此一举呢?

If I remember correctly from Java SE, any references that are isolated & not connected to a running program & can be garbage collected anyways. So does this make the above superfluous?

在另一方面,在移动设备中是不同的生命周期,将上述是一个最佳实践?

On the other hand, the lifecycle in mobile devices being different, would the above be a best-practice?

我知道它的不能伤害的做到这一点,但有时也有一些类变量(引用indivudual UI元素等),所以我真的很想知道我自己的理解,到底发生了什么的。

I know it cannot hurt to do it, but sometimes there are a number of class variable (references to indivudual UI elements etc), so I'd really like to know for my own understanding, what's really going on.

在此先感谢!

推荐答案

您有它正确的时候,你说,垃圾收集器将拿起被隔离的参考。具体地,未连接到执行线程引用的任何图将被收集。正因为如此,也没有充分的理由的变量设置为null,我可以看到。任何优势将由code维修成本远远超过。

You had it right when you said that the garbage collector will pick up references that are isolated. Specifically, any graph of references not connected to the execution thread will be collected. Because of this, there is no good reason to set your variables to null that I can see. Any advantages would be far outweighed by the code maintenance cost.

这篇关于的onDestroy() - 设置或不设置实例变量为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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