1
interface Animal { void makeNoise(); } 

class Horse implements Animal {
  Long weight = 1200L; 
  public void makeNoise() { System.out.println("whinny"); } 
} 

public class Icelandic extends Horse {

  public void makeNoise() { System.out.println("vinny"); } 
  public static void main(String[] args) { 
    Icelandic i1 = new Icelandic(); 
    Icelandic i2 = new Icelandic(); 
    Icelandic i3 = new Icelandic(); 
    i3 = i1; 
    i1 = i2;
    i2 = null;
    i3 = i1; 
    //<-- HERE
  } 
} 

When HERE is reached, how many objects are eligible for the garbage collector? And why?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Sucheth
  • 11
  • 1
  • There's a code formatter built into Stack Overflow, but you didn't use it (hint, use the `{}` button). And the line numbers were defeating it too (so I've put a comment in to indicate line 15 instead) – Damien_The_Unbeliever Apr 16 '13 at 06:54
  • 2
    What do *you* think the answer is and why? Tell us what *you* think, and we'll tell you if we agree or not. – JB Nizet Apr 16 '13 at 06:56
  • @JBNizet - Good point! I've deleted my answer until I see a response from the OP. – Stephen C Apr 16 '13 at 07:05

4 Answers4

1

Four objects can be cleaned up. There are two Icelandic objects which are no longer referenced and two Long objects. One Icelandic and one Long and the String[] can still be referenced.

Note: future versions of Java could auto-box Longs like 1200L in which case the answer would be just 2 as the Longs would still be referenced. e.g. you changed the example to Integer the answer would depend on command line arguments.

BTW: I didn't know Icelandic was a type of Horse. Perhaps IcelandicHorse would be less confusing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

An object becomes eligible for garbage collection in Java on following cases:

  • References of the object set to null.
  • Object is created inside block and reference goes out scope once control exit that block.

In general, an object becomes eligible for GC if it's not reachable from any live threads or any static references.

Now, after clarifying this, you should be able to do the calculation and answer the question.

      ____________________________________
     |            ___              ___    |
     |  x------->|:) |    y------>|:) |   |
     |           |_X_|            |_Y_|   |
     |____________________________________|  

Now lets say x is set to null:

     _____________________________________
    |            ___              ___     |
 null<------ x  |:( |     y----->|:) |    |
    |           |_X_|            |_Y_|    |
    |_____________________________________|

Then there are no more references pointing to X, so it becomes eligible for GC.

Note that if there was another variable (e.g. x2) pointing to X, it will not become eligible for GC after x will be set to null, since x1 will still point to it.

Tell us what do you think after the clarifications, and we'll discuss your answer.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • The `set to null` comment is confusing. Setting a reference to null if the object can be reference elsewhere does noting. In this case setting to `null` makes no difference. – Peter Lawrey Apr 16 '13 at 07:02
0

2 Icelandic instances can be collected, since only the second instance is still referenced. Same goes for the Long weight. You can use VisualVM to monitor object allocations, manually activate the GC and see what's left.

0

Two object is eligible for garbage collection by considering the fact that you are still in that scope HERE.

Object created by i1 & i3 is eligible for garbage collection.
Object created by i2 is not eligible.

System.out.println(i3 == i1);  

Prints true. both reference are referring to same object. And other two objects are eligible for garbage collection.

AmitG
  • 10,365
  • 5
  • 31
  • 52