Viroth
Viroth

Reputation: 702

Android Garbage collector

Example I have create new object on onCreate Event in activity like :

Object object = new Object();

I have asssigned objct to null, in order to prevent leak memory.

Is it good way to set it null in onDestroy event?

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

Upvotes: 2

Views: 151

Answers (1)

Israel dela Cruz
Israel dela Cruz

Reputation: 806

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. In android, Activity must not be referenced outside its own or its subcomponents. 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.

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().

Upvotes: 2

Related Questions