• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC doubt

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it from
http://www.danchisholm.net/oct1/mybook/chapter16/exam1.html

class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

Answer is g.
Exlanation is : Please note that this question asks which object is NOT eligible for garbage collection after method m1 returns. The objects referenced by i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1 references any of those objects. When method J.m1 returns, the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects.


But i drawn the diagram i found non of the objects are eligible as objects other is pointing to other object like in statement i1.other(i3);
i1's other is pointing to i3 and not i1 is pointing to i3
which holds true for every other statement. so all the referances will be there. so no object is eligible for GC.

Also please suggest me how to draw diagrams here to post .. Because just now i try to draw in word but after that i couldn't able to cut and paste that diagram here.. so all my energy went waste.. so please help me out..

Thanks,
Geeta Vemula
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the objects are local to the method.
When a method returns all are eligible for GC

AND also the new object in main() is also Eligible after the method returns
[ December 23, 2008: Message edited by: James Tharakan ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
geeta vemula , you have to upload you image in the net and provied the link using URL tag.
You can use flickr
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so its because the method returns or island of isolation?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is: Which object is "not" eligible
As James said, when m1 returns, all objects created in m1 "are" eligible. Moreover, the object created from main to invoke m1 on will also be eligable at this point because it was not assigned to a reference. As such, the main thread can't get to any object, hence "all" are eligible.

So the answer to which object is "not eligible" at this point, is ... none, because they all are eligible.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geeta vemula:
so its because the method returns or island of isolation?



Its because of method returning.

For the island isolation, with respect to i1,i2,i3
you have to set
i1=i2=i3=null;
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I tried to make the diagram for this example...
Here stack and heap are shown in short.




And after the end of m1() method....


[ December 23, 2008: Message edited by: punit singh ]
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wowww punit.. great job.. now my doubt is cleared entirely.. How did you draw this( i mean which editor)??

Thanks punit and james for clearing my doubt.

Geeta V.
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good, asside for not reflecting J object.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its word 2007.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Punit,

could you explain how (i1.other(i3), i2.other(i1),
i3.other(i2), i4.other(i4) ) this method refers to each other objects?

since this is a method, i am confused...

please clear this too..

Thanks
Preetha
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@James
@Ankit, you have also mentioned in one of your past posts/threads.

What is flickr? Is it a tool for uml modeling? If it is an open source product, then provide the url, please. I will appreciate it.

I am sorry as this question doesn't pertain to the subject of this thread.

Thanks,

Naveen

[ December 23, 2008: Message edited by: nav katoch ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arun for methods object references are stored with methods on the stack, but actual object are stored in heap, that is referred by references from the stack. I have shown this on the first diagram, see it closely.

I have shown I1, I2, I3, I4 references on the stack frame while Actual Objects are shown on the heap segment.

There is nothing new here Arun.

i1.other(i3);


I class has reference of itself so it can store another I in itself, as shown in first diagram every block has an I reference to refer an object of I.so i1.i will refer to an object referred by i3 reference.
like: i1.i=i3.

i2.other(i1);


same way i2.i will refer to the same object that is referred by i1 reference
like: i2.i=i1;

i3.other(i2);


same way

i4.other(i4)


same way.

The only difference is that here objects are created in the method, so they are local/automatic object, that's why their reference will be stored in the method on the stack.

For Objects create in the class, means for instance variables of the class, their references are store in the class' object instance in the heap.

Only difference is the location of references.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nav katoch:
@James
@Ankit, you have also mentioned in one of your past posts/threads.

What is flickr? Is it a tool for uml modeling? If it is an open source product, then provide the url, please. I will appreciate it.

I am sorry as this question doesn't pertain to the subject of this thread.

Thanks,

Naveen


[ December 23, 2008: Message edited by: nav katoch ]




Naveen flickr is a photo sharing website, its url is flickr.com, just upload your images there, take the url of your images and add them with image tag while posting. I have done the same thing.
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Punit.
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic