• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection?

 
Joe Harry
Ranch Hand
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Uppeteer
Ranch Hand
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kalpesh Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Joe Harry
Ranch Hand
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Harry
Ranch Hand
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
Mark Uppeteer
Ranch Hand
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not able to see the image. Make sure it is added to the post.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see it.

I like your baroque arrows!

Bu.
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic