Page 1 of 1

About Question enthuware.ocajp.i.v7.2.922 :

Posted: Tue Feb 16, 2016 4:19 pm
by assembler07
An object should be eligible for garbage collection only when his reference becomes pointing to null ?

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Tue Feb 16, 2016 7:10 pm
by admin
In general, if there are no references pointing to an object then it is eligible to be garbage collected. But it is a bit more complicated than that. Please go through the following links to understand better:
http://beginnersbook.com/2013/04/java-g ... ollection/

http://www.oracle.com/webfolder/technet ... index.html

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Fri Feb 16, 2018 9:09 am
by Rohit Singhania
Yes. The process is called as Garbage Collection. In this process compiler identifies the objects which are in use and which are not, and deletes the unreferenced objects that is no longer referenced by any part of program.

I would like to suggest refer below resource to understand garbage collection better,

https://www.flowerbrackets.com/garbage- ... n-in-java/

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Sun Sep 09, 2018 3:02 pm
by herbertscbr
After line 10? Why not in line 10?

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Sun Sep 09, 2018 9:44 pm
by admin
Only when the execution of the line is completed, can its effect be seen. So, the object can't be GCed, while line 10 is being executed. Thus, "after" and not "in".

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Sun Sep 09, 2018 9:59 pm
by herbertscbr
Sorry, but and the line 6?
(I understand what you said about line 10.. )

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Sun Sep 09, 2018 10:27 pm
by admin
I am not sure what is your question about line 6. Can you be a bit more clear?

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Mon Sep 10, 2018 8:40 pm
by herbertscbr
In the following code, after which statement (earliest), the object originally held in s, may be garbage collected ?

1. public class TestClass{
2. public static void main (String args[]){
3. Student s = new Student("Vaishali", "930012");
4. s.grade();
5. System.out.println(s.getName());
6. s = null;
7. s = new Student("Vaishali", "930012");
8. s.grade();
9. System.out.println(s.getName());
10 s = null;
}
}


6. s = null; //right answer
10 s = null;//to me, this answer is correct too

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Mon Sep 10, 2018 9:25 pm
by admin
The problem statement is asking about "object originally held in s". This object is eligible for gc after 6.

Re: About Question enthuware.ocajp.i.v7.2.922 :

Posted: Tue Oct 01, 2019 11:20 pm
by reekjohns
Generally, an object becomes eligible for garbage collection in Java on following cases:

* Any instances that cannot be reached by a live thread.
* Circularly referenced instances that cannot be reached by any other instances.
* If an object has only lived weak references via WeakHashMap it will be eligible for garbage collection.
* The object is created inside a block and reference goes out scope once control exit that block.