aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on GC from Dan's exam 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 "Question on GC from Dan Watch "Question on GC from Dan New topic
Author

Question on GC from Dan's exam

Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
i found the folwwing Q:

I thaught that ans would be a and c but i was wrong.the given correct ans is a,b,c...
Plz explain me why?
thanx in advance
regds
Arpana


SCJP1.4(91%)
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
I think the ans is a,b,c because the variable i1,i2,i3 are all local variables,they only exist in the method scope,when the method end,they will be eligible for gc.so...
if I am wrong correct me,pls.


scjp 1.4<br />challenge haven't limit!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The reason all three of the objects are eligible for garbage collection is because there are no references to them from "active" parts of the application.
When the method m1 is invoked, three new objects are created and references to them are assigned to i1, i2, and i3. Then, each of those objects is given a reference to each of the others. At this point the reference count for each object is 2.
When the method exits, however, the variables i1, i2, and i3 go out of scope. Now, the reference count for each of the created objects is 1, but those references are from objects that can no longer be accessed. This makes them eligible for garbage collection.
Corey


SCJP Tipline, etc.
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Thanx Corey, it is clear now.
regds
Arpana
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
hi,Corey
if I change the modifier of the variable other into public,as follows:

the ans will be what? d?
pls!
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by ji dragon:
hi,Corey
if I change the modifier of the variable other into public,as follows:

the ans will be what? d?
pls!

The answer will not change if you make other public. The 3 objects will still be eligible for garbage collection after m1() completes. The explanation given by Corey still applies: There is no active part of the application that has references to any of the 3 objects.
However, if you make other static, only 2 objects will be garbage collected, because the I class itself will still hold a reference to one of the objects.


SCJP 1.4
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
Originally posted by Corey McGlone:
but those references are from objects that can no longer be accessed.
Corey

why?
[ December 10, 2002: Message edited by: ji dragon ]
[ December 10, 2002: Message edited by: ji dragon ]
Robert Skowronek
Greenhorn

Joined: Jun 03, 2002
Posts: 1
> why?
Because after m1() method completed you have no reference to object of J class.
-> new J().m1();
In case were the code whould be:
J ref = new J().m1();
the correct answer should be d).
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Robert Skowronek:
> why?
Because after m1() method completed you have no reference to object of J class.
-> new J().m1();
In case were the code whould be:
J ref = new J().m1();
the correct answer should be d).

First, the code you gave will not compile. I think that you meant:
J ref = new J();
ref.m1();
Even in this case, the 3 objects of class I will still be eligible for garbage collection after the execution of method m1. The ref object itself does not hold references to the I objects, the local variables in method m1 hold references to the I objects. Once the m1 method completes, the local variables are discarded, so there are no more "live" references to the I objects. The I objects hold references to each other, but as Corey explained, that does not prevent them from being garbage collected.
Hope that helps.
[ December 10, 2002: Message edited by: John Paverd ]
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
hi,John,
I am sorry I still not clear, what I can't understand is "The I objects hold references to each other, but as Corey explained, that does not prevent them from being garbage collected."
why? Since there are references for each of the created objects,why those references are from objects that can no longer be accessed.I think I can access them by reference for class I.eg:I ii=new I();I.other...
please explain it to me,thanx very much!
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by ji dragon:
hi,John,
I am sorry I still not clear, what I can't understand is "[qb]The I objects hold references to each other, but as Corey explained, that does not prevent them from being garbage collected."
why? Since there are references for each of the created objects,why those references are from objects that can no longer be accessed.I think I can access them by reference for class I.eg:I ii=new I();I.other...
please explain it to me,thanx very much! [/QB]

Ji Dragon
In the original example in the very first post, what code could you add after
new J().m1();
to access the objects that were created in m1()?
There is no way to access these objects any more. That's why they are eligible for garbage collection.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Ji,
In such a case, I think the use of pictures are highly underutilized.
Let's run through the original example, and look at how the objects are created in memory and the references to them. Here's the original code snippet, for reference:

When we invoke the method m1, we create three new objects and assign references to them to the three variables of type I: i1, i2, and i3. At this point, our objects and references (the references are notated by arrows) look like this:

Next, when we execute the following three lines of code, we're adding references to each of the objects from the others. At this point, our objects and references look like this:

As you can see, each object now has two references to it. However, once the method m1 completes, the variables i1, i2, and i3 go out of scope. So, taking that into account, our objects and references look like this:

Notice that there is now no way to access any of these objects. There is no reference from an active part of the application that will allow you to get to any of the objects that have been created. Therefore, even though there are references to each object (in a circular fashion), they are still eligible for garbage collection.
I would also urge you to check out this Flash application that I made. I really made it to display how objects are passed to methods in Java, but it also illustrates some garbage collection principles.
I hope that helps,
Corey
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
aha,I get it now!
thanx,guys!
Pradeep Sahoo
Ranch Hand

Joined: Oct 13, 2002
Posts: 46
Thank you Corey for the excellent stuff.
I havent seen anybody else take the effort to explain such concept.
Thanks once again


Pradeep<br />Consultant - Enterprise Solutions,<br />Infosys Technologies Ltd,<br />Bangalore, India
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on GC from Dan's exam