• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on GC from Dan's exam

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Corey, it is clear now.
regds
Arpana
 
dragon ji
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,Corey
if I change the modifier of the variable other into public,as follows:

the ans will be what? d?
pls!
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
dragon ji
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aha,I get it now!
thanx,guys!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Corey for the excellent stuff.
I havent seen anybody else take the effort to explain such concept.
Thanks once again
 
Evildoers! Eat my justice! And this tiny ad's justice too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic