简体   繁体   English

Android垃圾收集器

[英]Android Garbage collector

Example I have create new object on onCreate Event in activity like : 示例我在onCreate事件中创建了一个新对象,例如:

Object object = new Object();

I have asssigned objct to null, in order to prevent leak memory. 我已经asssigned objct为空,以防止泄漏内存。

Is it good way to set it null in onDestroy event ? onDestroy事件中将其设置为null是一种好方法吗?

@Override
protected void onDestroy() {
    super.onDestroy();
    object = null;
}

It is not a question of "is it good", it's a question of is it worth the time writing it? 这不是“它是好的”的问题,这是一个值得花时间写它的问题吗? Well, no. 好吧,不。

It's because of Android way of writing code. 这是因为Android编写代码的方式。 In android, Activity must not be referenced outside its own or its subcomponents. 在android中,不得在其自身或其子组件之外引用Activity This way, when the onDestroy() is called, Android can let go of the last reference to Activity allowing the GC to collect it and all of its objects, including your object . 这样,当调用onDestroy() ,Android可以放弃对Activity的最后一个引用,允许GC收集它及其所有对象,包括你的object

What you should put your time on is to figure out how to stop all the background threads that you started in the Activity and stop them at onDestroy() . 您应该花些时间来弄清楚如何停止在Activity启动的所有后台线程并在onDestroy()处停止它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM