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

question on garbage collection........

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the following question which i am posting.....was given to me by my friend who is also preparing for scjp6.0..........




// On garbage collection
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



from my point of the ans should be d........
so help me through this.......
and let me know whether i am right or wrong........
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeevitesh Singh:



// On garbage collection
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



Hello Singh...

1.
i1 -> i1; i1.other -> i2;
i2 -> i2; i2.other -> i1;
i3 -> i3; i3.other -> i3;

2.(i1 = i3; )
i2.other -> i1; // 'i1' is not eligible for garbage collection

3.(i2 = i3; )
i2 -> i3; // now 'i1' and 'i2' is eligible for garbage collection

My answer C;
[ October 10, 2008: Message edited by: Turbold Dugarsuren ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the answer is C.

When the instance of J is created, three instances of I are created, A B, C.
J has a valid reference to each of these.


When m1 executes the internal reference of A (other) is set to B, and B to A. C is set to itself.

the second line sets J's references i1 and i2 to i3, i.e. all of J's references now point to C.

This means that although A and B hold references to each other there is no external reference
to either of them therefore they are unreachable and potentially eligible for gc.

J still has valid references to C so it is NOT eligible.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think both A & B become eligible for garbage collection according to the code as they become island of isolation (i.e: they don't have references from outside but referencing each other). C is not eligible for GC. Since both i1 & i2 references to C.
 
Jeevitesh Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mr.anderson and mr.mglboy......
i still believe the ans is (d)......
coz i1 refers to its object which has a reference(name) to a string ,and
one refence(other) of its own class object...and same with i2 and i3...
when i1.other is said to i2 and when i2.other is set 2 i1......
and i3.other is set to i3 object but it does not mean that i3 has stopped
pointing to its original object.....so that object still has a live
reference.....if i3 has it then string also is with a live reference reference.......
what say.....
reply..........
 
dee anderson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be confusing yourself with I pointing to strings. The issue here is not what I holds a reference to, but what holds a reference to I.

If there is no executing code which can reach an instance of I through a reference variable then that object becomes eligible for gc.

Add some code into I to return a name and put some code in m2 to print out i1.name, i2.name and i3.name.

Try to find some way of accessing A or B objects in m2, you will find this not possible because J has no valid references any more.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mglboy, please check your private messages. You can see them by clicking My Private Messages.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class I {

private String name;

public I(String s) {
name = s;
}


private I other;

public void other(I i) {
other = i;
}

}


class J {

// at this point
// three object created
// object #1 referenced by i1
// object #2 referenced by i2
// object #3 referenced by i3
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");

// at this point
// object #1 have two references with i1 and i2
// object #2 have two references with i2 and i1
// object #3 have two references with i3 and i3
private void m1() {
i1.other(i2); i2.other(i1); i3.other(i3);

// here !!!
// object #1 lost references with i1
// object #2 lost references with i2
i1 = i3; i2 = i3;
m2();
}

private void m2() {
/* Do amazing things. */
}

public static void main (String[] args) {
new J().m1();
}

}

This way, only object C is NOT elegible for GC
[ October 10, 2008: Message edited by: Jos´┐Ż Vieira ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
To me it seems references of A,b and C are stored in string pool, as they are compile time. Therefore they will never be garbage collected.
please make it clear if i am wrong.

Regards
Sunny
 
Jeevitesh Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello sunny....
i also feel the same but the answers say that it should be
A and B....
they are saying j doesnt have a reference if thats the case then from my point of all 3 should be garbage collected.....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic