Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I am doing Diagnostic Exam from Devaka's Exams. I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection after Line 12....

Answer is : 1

 
Rafael Angarita
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you initialize a2 at Line 8 you are passing a new object A to the constructor and then, the instance variable "a" of a2 refers to that new object.
In Line 12, that instance variable is set to null, so there is no reference to that isolated object.

The rest of the objects still have a reference to them.

(I think I'm having problems explaining myself :lol: )
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me draw an ASCII map of what is happening




When a1.a=new A(null); is executed, this happens



and after execution of a2.a=null; //Line12 , this happens

 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
and after execution of a2.a=null; //Line12 , this happens


Its a2.a=null;
So this should happen


Banu Chowdary wrote:
I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection

The way Ankit explained it..........that's exactly the way to go about solving GC questions.
Always draw the objects on paper as they are created and make a cross when their reference is removed in the code.
In such a way, in the end you can see how many objects are left isolated.
If Ankit can do it in the message, you should be able to do it on paper.......
Practice a lot(on paper). You should do fine after some experience.

Ankit,
You really explain well, such types of questions......on the forum....
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sachin for correcting my mistake
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is the Object in line 1 eligible for garbage collection???

the finalize method is running and printing output "-" . I already posted a question about garbage collection. But still in confusion....


Is the Object in line 1 available for Garbage Collection???
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN both the code the object created is eligible for GC. because there was only one reference and that is also set to null.
The only difference is that in first code you are requesting GC to do its work.
I the second one. Its left to GCr. It would do if it feels like doing GC...
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James.. Now Cleared..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic