• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Garbage Collection Problem Explain Pls Dan Chisholm single topic

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Have problem understanding the answer to question below Anyone can give a line by line explanation how i1, i2, i3 ends up referring to each other? What abt i4?
Your help most appreciated.
Q from Dan Chisholm single topic exam on GC.
http://www.danchisholm.net/dec20/topic/section3/gc1.html
Question 4
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? Ans: g. None of the above
Ans:
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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After the

you have four objects on the heap, referenced by i1, i2, i3, i4.
And after this part of the code:

you'll have i1's field "other" pointing to i3 (i1.other->i3), i2's to i1 and i3's to i2 (these 3 will form a ring-like structure). i4.other will reference the same object the i4 itself is referencing.
But the key point here is to read the question well:
"Which object is not eligible for garbage collection after method m1 returns?"
So the answer is "None" (double negation!) or to be more clear "They're all eligible for GC AFTER the method m1 returns".

HTH.
[ February 24, 2004: Message edited by: Bojan Knezovic ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right - I think you're being hung up on the grammar of the question, not on the question itself. You obviously understand garbage collection in this instance, but the semantics of the question seem to have gotten in your way.
The question asks which of the following is NOT eligible for garbage collection. Therefore, in order for you to answer a, b, c, or d (which equate to i1, i2, i3, and i4, respectively), one of those objects would have to be ineligible for garbage collection. Obviously, that's not the case. Therefore, g is the correct answer.
 
Rob Kiu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Bojan & Corey,
Thanks a lot, your explanations certainly helps.
Got a bit confused by i1.other(i3); i2.other(i1); i3.other(i2); i4.other(i4);part and how they end up referring to each other, luckily not by the NOT eligible part. Bojan cleared it up with his "ring like structure" explanation.
Will try more GC questions, seems my weak area.
Cheers,
Rob
 
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
When dealing with garbage collection, I find that the old cliche, "A picture is worth a thousand words" is very true. Almost any time I'm faced with a question about garbage collection, I find that drawing a picture is the easiest thing to do in order to understand what is going on.
I drew a quick example of this question and put it online here. If you look at that picture, you can see very easily how each object is caught in a circular reference and is, therefore, eligible for garbage collection.
I hope that helps,
Corey
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone-
The following is from the same exam- SectionIII- q8

now, when i'm passing an object reference to a method, the method receives a copy of the reference and any changes done to the reference reflect to the object whose reference was actually passed right?? the following is from Kathy's book

now the bold part says any changes to the object inside the method are being made to the actual object reference passed. now in Dan's exam, why does the progam print out "X" when in m1() the object reference was set to null and the output should've been a NullPointerException. (I ran both the programs, they run as they say. and also for kathy's example, i set
in the modify method and it prints out
meaning it has not nulled the reference.
can anyone explain plz
Edited by Corey McGlone: Cleaned up formatting
[ February 24, 2004: Message edited by: Corey McGlone ]
 
Shahfazal Mohammed
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and sorry for messing up the format of this thread..i just copied and pasted into the textarea..dunno what went wrong
 
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
First of all, Shahfazal, it is generally considerate to start a new thread when asking a new question. I realize that this question comes from the same mock exam, but it's really a new question and warrants a new thread.
However, you already have the answer to your question - you just don't realize it. You said it yourself - when you pass a reference variable to a method, that method receives a copy of the reference.
At that point, both the calling block and the called block have a reference to the same object, but those references are different variables - they are copies of one another. Therefore, if you modify the object referred to by either reference, you'll see that change reflected via the other reference because they both point to the same object.
However, if you change the reference itself (as is the case when you set it to null), you are only impacting one copy of the reference, not the object that the reference refers to.
I think you might find the following demonstration useful: Link.
I hope that helps,
Corey
 
Shahfazal Mohammed
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouch! deserved that rap. forgot that this is a different concept frmo the actual question that started this thread.
Anyway, thanx a lot.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I am new to the forum and this is my first post.
I would really appreciate if someone can help me .
Question 4
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? Ans: g. None of the above
********

i did understand that
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
creates 4 objects on the heap of type I, refered by i1,i2,i3,i4.
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
now i don't get this part.
i1.other referes to the same object as i3 does
i2.other referes to the same object as i1 does
i3.other referes to the same object as i2 does
i4.other referes to itself
but does it mean i1,i2,i3,and i4 do not refere to any object now?
these references still refer to the original objects right? [/LIST][LIST]
 
Roopa Gowda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the output of i1 was same .
does it mean that i1 is still refering to the object originally created?
 
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

Originally posted by Roopa Gowda:
i did understand that
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
creates 4 objects on the heap of type I, refered by i1,i2,i3,i4.
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
now i don't get this part.
i1.other referes to the same object as i3 does
i2.other referes to the same object as i1 does
i3.other referes to the same object as i2 does
i4.other referes to itself
but does it mean i1,i2,i3,and i4 do not refere to any object now?
these references still refer to the original objects right?


There is a subtle yet very distinct difference between what is going on and what you said.
i1, i2, i3, and i4 all refer to objects of type I. However, each of those objects contains a reference variable which refers to some object of type i.
Therefore, after we have invoked these lines:


i1.other(i3);
i2.other(i1);
i3.other(i2);
i4.other(i4);


We have a case where i1, i2, i3, and i4 all still refer to the same objects they referred to in the beginning. However, i1.other refers to the object referred to be i3, i2.other refers to the object referred to by i1 and so on.
Be sure to look at the picture I drew of the situation here. Hopefully, looking at that picture can help you understand what is going on. If you have more questions, let me know.
 
Roopa Gowda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton.
so i1,i2,i3,i4 are local to the method and the objects r left with no references outside the method,hence eligible for gc
hope i am correct this time.
 
What's that smell? I think this tiny ad may have stepped in something.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic