• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection - making the object eligible for GC.

 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Experts/Folks,

I have a small doubt regarding making the object eligible for GC.

We can make an object eligible for GC by referencing it to NULL in the end. (This is one approach)



Now, after step 3 we are explicitly making the object s1 eligible for GC.

Also, i know that once the method execution is completed all the objects which are no more referenced implicitly become eligible for GC.
If in the end they implicitly become eligible then what is the use of referencing it to NULL in the end. Isn't it simply making the code file bulkier?

Is it advisable to reference the object to NULL in the end.

Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First of all, the GC collects objects when they become unreachable -- not when a reference get set to null.

Setting a variable to "null" will not make the object unreachable, if there are other reference variables pointing to the same object. Also, having a variable go out of scope will not make the object unreachable, if there are other reference variables pointing to the object too.


To answer your question, setting a reference variable to null, just prior to the variable going out of scope, is not needed.

Henry
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
himanshu.harish agrawal wrote:Hello Experts/Folks,

I have a small doubt regarding making the object eligible for GC.

We can make an object eligible for GC by referencing it to NULL in the end. (This is one approach)



Now, after step 3 we are explicitly making the object s1 eligible for GC.

Also, i know that once the method execution is completed all the objects which are no more referenced implicitly become eligible for GC.
If in the end they implicitly become eligible then what is the use of referencing it to NULL in the end. Isn't it simply making the code file bulkier?

Is it advisable to reference the object to NULL in the end.

Thanks.


In a method- Once the method complete its execution- the local reference variables used therein will be removed from the stack. So the instances created in the method are then eligible for GC (provided they arent returned by the method or there is some other reference to that instance). As said by Henry, it all depends if the instance can be reached via some reference. Also there are some cases when there's is an Island of instances- each pointing to each other. But there's no way one can reach that instance from the code.

 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
First of all, the GC collects objects when they become unreachable -- not when a reference get set to null.

Setting a variable to "null" will not make the object unreachable, if there are other reference variables pointing to the same object. Also, having a variable go out of scope will not make the object unreachable, if there are other reference variables pointing to the object too.


To answer your question, setting a reference variable to null, just prior to the variable going out of scope, is not needed.

Henry


Thanks for the reply. OK, so whether you set it to NULL or not will not help in any way in easing the process of GC. Or is it that if it is set to NULL then it is more likely to get removed by GC (consider you have not reassigned the object to some other object reference variable or created an island of instances from it)?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic