This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection?" Watch "Garbage Collection?" New topic
Author

Garbage Collection?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Guys,

Consider the code below,



I would be very much thankful to the guy who will explain me with the help of a diagram for the above question on how many objects will be eligible for gc??

I'm not understanding as to how this works. Please help me guys. I always go wrong with island of isolation. Guys please help me.

Thanks in advance.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

All of them.

the garbage collector will (when it runs, if it feels like it), after your method, it will clean all of them.

Best way I find to draw it is: it write down the name of the reference var and I draw a circle for the object on the other side of my piece of paper with the name of the reference in it. Then I connect both of them. And like this I go through the exercise. (circles(=objects) can point to other circles) If at a certain point I'm asked how many objects are eligeble for GC, then I just count the number of unconnected circles (or islands) ands those can be GC'ed.
In your case, your reference vars are destroyed after the method since they go out of scope and all your objects are eligeble of GC.

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Yes, all of them, once your program exits. Otherwise, before an exit, all your objects can be accessed by a live thread, so do not see any being eligible.

Perhaps, if i could add my 2 cents to your confusion, try nulling the references i1, i2 and i3. What you get is an island that no ship(a live thread in this case) can reach.


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Kalpesh Jain
Ranch Hand

Joined: May 29, 2006
Posts: 30

I would be very much thankful to the guy who will explain me with the help of a diagram for the above question on how many objects will be eligible for gc??

Jothi Shankar, Its important to mention that after which line we are looking for garbage collectable objects



All of them.

the garbage collector will (when it runs, if it feels like it), after your method, it will clean all of them.

Mark, it will be interesting to know the count of eligible gc objects
[ December 05, 2006: Message edited by: Kalpesh Jain ]

Its a j a v a
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Kalpesh,

I'm seeing some sign that you are going to help me. I want to know when we reach new J().m1();

Can you please draw the diagram.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,
at the end of the method, all four objects still have a reference. The code pretends as if this is an island of isolation, but it isn't. They are still referenced from outside.

Anyway, as they are local they will be doomed after the method ends.

Ok, diagrams:



after
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();


after
i1.other(i3); i2.other(i1);



after
i3.other(i2); i4.other(i4);


all objects are still referenced (within the scope of the method).



Yours,
Bu.
[ December 05, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Bu,

I forgot that you are there in this forum. I remember that you helped me with one similiar diagram before. I thank you very much for the help. That diagram above really helped me. Thanks again for the initiative.

Thanks again.
[ December 05, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
jimmy halim
Greenhorn

Joined: Nov 28, 2006
Posts: 15
How about the idea checking is there any way to get the object reference.
If there's no way the get the object through variables, arrays, collections, etc then the object is eligible for garbage collection

From the example if we want to check that object that created at : "I i1 = new I();" . Is it eligible for garbage collection after calling method m1(), just check if you can get any reference to it.

Because an object is eligible for garbage collection is when no live thread can access it.

Drawing diagram that Mark said also a great way to describe references, my idea is just another fast and simple way (although maybe dangerous )


Knowledge is power<br />My Blog : <a href="http://jimmod.blogspot.com/" target="_blank" rel="nofollow">http://jimmod.blogspot.com/</a>
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

The way I was explaining in my first post leads to this (and this is the way I'll solve my exercises on the exam to). (This is the first time I try the image thing so I hope I do this right )



to see at a certain point how many objects are eligible for gc, count the objects on the heap that have no 'way' to the left side of the line. If you find a path to the object (following the arrows), then it is still ok, if not it is eligible for gc.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

I'm not able to see the image. Make sure it is added to the post.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
I can see it.

I like your baroque arrows!

Bu.
Greg L Tonn
Ranch Hand

Joined: Nov 27, 2006
Posts: 52
Mark, I do it the same way but on a recent S&B self test question I saw that it's prone to mistakes.

If an object, we'll call it a1 has references to other objects like a Short (wrapper) and then a1 is dereferenced there will be two objects available for GC.

Hopefully they don't get this tricky on the real exam!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

I'm able to get it now. This one looks good, but I feel comfortable with Bu's explanation as it sequentially shows how and what happens.

I thank all those took this much effort. Thanks again.
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Hi
Greg L Tonn,
if you have an island of 2 objects on the right side of the line, then you can see they are a GC'able island no ? since they are not connected to the left side of 'the line'. If I should make a mistake, please tell me.

I like this way because I can do it all in one drawing.
Anyway, each his methods

Glad the arrows are appreciated, they took me a lot of time, and of course you can understand that they are copyrighted.

regards,
Mark
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Hi
Greg L Tonn,
if you have an island of 2 objects on the right side of the line, then you can see they are a GC'able island no ? since they are not connected to the left side of 'the line'. If I should make a mistake, please tell me.

I like this way because I can do it all in one drawing.
Anyway, each his methods

Glad the arrows are appreciated, they took me a lot of time, and of course you can understand that they are copyrighted.

regards,
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection?
 
Similar Threads
Garbage Collection Problem Explain Pls Dan Chisholm single topic
Garbage collection problem
Dan's question
what is island of isolated objects
Garbage Collection