GeeCON Prague 2014*
The moose likes Java in General and the fly likes Garbage collection - making the object eligible for GC. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage collection - making the object eligible for GC." Watch "Garbage collection - making the object eligible for GC." New topic
Author

Garbage collection - making the object eligible for GC.

himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

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)?
 
GeeCON Prague 2014
 
subject: Garbage collection - making the object eligible for GC.