Pushkar
Pushkar

Reputation: 727

For Garbage Collection is it good idea that if we make object as null and clear collection after use at the end of method

For data load i am using groovy script. where I am using multithreading concept. But when i start data loading after certain interval data load get stopped and JVM start doing Garbage collection. My question is will it be good idea that set object as null and clear collection in method after use at the end of the method?

Other best approaches are well come Thanks in advance.

Upvotes: 0

Views: 626

Answers (3)

raj
raj

Reputation: 21

In multithreading try to define variables globally write as possible as local variable with in the method and inside the loop or if condition depending their scope of use.

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79828

It depends on what type of variable you're using to refer to the object.

  • If the variable is local to that method, and there are no other references to that object, then it doesn't help you to set the variable to null, as it is going to go out of scope anyway.
  • If the variable is a member of the class, and the object that you're running the method for is going to be sticking around for a while, then it will make a difference to null out the variable if you've finished with that object; because the variable will still be in scope for code within your class. The object won't be garbage collected if there's a reference to it that can be reached somehow.

Upvotes: 2

enterbios
enterbios

Reputation: 1757

It does not make a difference to garbage collection. Once your method is over and no more references to object/collection exists GC will be able to clean them. It does not make a difference if object/collection become inaccessible because you nulled a reference or method which locally hold last reference has finished.

Upvotes: 1

Related Questions