• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Dan exam doubt 18

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii in Garbage collection questions...in dan exam

---------------
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


the answer is "g"
with explanation given asPlease 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.


i m not able to get the answer ? and explanations...

in k&B book ...its written that island of isolation are eligible for Garbage collection

but here ...its different
???

pls expain me...
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhh ufff

i think all objects become eligible for gc...

i got confused with the question "NOT"

so does it means that which object does not become eligible for gc
and "none of above" answer is saying that none of above are not eligible for gc
is i m rt ?

so finally island of isolation do become elligible for gc ?

pls clear me
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okkk do clear me question 2nd
=====
2nd
=====

in this

class I {
private String name;
public I(String s) {name = s;}
private I other;
public void other(I i) {other = i;}
}
class J {
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
private void m1() {
i1.other(i2); i2.other(i1); i3.other(i3);
i1 = i3; i2 = i3;
m2();
}
private void m2() {/* Do amazing things. */}
public static void main (String[] args) {
new J().m1();
}}

Which of the three objects, A, B or C, is not eligible for garbage collection when method m2 begins to execute?

a. A
b. B
c. C
d. None of the above

with answer "c"

will the answer be "d" if question say "when m2 ends" ?
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi amit

in both the cases objects will become island of isolation objects.with no reference to the object from the outside world.

in the first case

obj i1 points to i3 and i3 points to i2 and which inturn points to i1.Thus forming ring of references.
i4 points to itself.

still there will be a reference to the i1 i2 and i3 and i4 from outside world.



In the second case

i1 and i2 have reference to each other.

i3 has its references from the i1 and i2.



if the isolation of objects are eligible for GC then in second case.i1 i2 and i3 are eligible for GC


i am wrong some where just comment
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer replied is v.confusing...

in my post i got the point of DAN but just ask for confirmation that is i m right or wrong wiht explanation... pls see my post...

thanx
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First question is corrected, none of above choice correct.
Every object will eligible for GC cause of an island of isolated objects.

So you can n't select one of them.

Second question.

At the last we will receive

i1 -> C, i2->C and I3-> C

Every variable point to C object.

Cause of this code

i1 = i3; i2 = i3;

It make i1,i2,i3 point to the same object in the heap.So A,B will lost.

C is correct.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer for the first question cause confusion. All objects are created in method m1(), so after m1() returns all objects are ellegible for GC. But because you don't have such answer, the correct will be "None of the above".

Amit, island of isolation is ellegible for GC, and the first question is an example of it, they created one to make you think that after m1() finishes, each object would have a reference to the other and none would be ellegible for GC.

As far as the second question, the only object reference that you're preserving is i3 (C), because for the other two (i1 & i2) you changed the reference before calling m2() method to the same object of i3. So the original objects referenced by i1 & i2 were lost. And that would happend before m2 begins or after it finishes, does not make any different because it didn't do anything else with the object reference. However, all will be ellegible for GC after m1() completes.

Hope this explanation is clear enough.

Regards,
Francisco
 
Hot dog! An advertiser loves us THIS much:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic