This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
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

Joined: Jan 09, 2009
Posts: 67
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: )


Rafael Angarita.
SCJP 6.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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....


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Thanks Sachin for correcting my mistake
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180


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

Joined: Aug 29, 2008
Posts: 580

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...


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Thanks James.. Now Cleared..
 
jQuery in Action, 2nd edition
 
subject: Garbage Collection
 
Similar Threads
help on Garbage Collection analysis
Doubt in Garbage Collection
Question about Garbage collection from ExamLab diagnostic Test
Garbage Collector doubt (Examlab Diagnostic Test) Q4
Garbage collection