File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC doubt" Watch "GC doubt" New topic
Author

GC doubt

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
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
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

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 ]

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

Joined: Aug 29, 2008
Posts: 580

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

Joined: Jul 18, 2008
Posts: 208
so its because the method returns or island of isolation?
Erez Pitchon
Greenhorn

Joined: Dec 19, 2008
Posts: 13
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

Joined: Aug 29, 2008
Posts: 580

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;
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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 ]

SCJP 6
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
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

Joined: Dec 19, 2008
Posts: 13
Looks good, asside for not reflecting J object.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Its word 2007.
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

@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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: May 02, 2008
Posts: 246

Thanks a lot Punit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC doubt
 
Similar Threads
Dan exam doubt 18
reg dan's test suite
Garbage Collection Problem Explain Pls Dan Chisholm single topic
islands of isolation
garbage collection