-1

Given:

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;
        System.out.println("end of program");
    }
}

how many objects are eligible for the garbage collector at the last statement (ie:where printing end of program)? The answer is 4 but i did not understand How they are 4 and there are at max 3 objects?

assylias
  • 321,522
  • 82
  • 660
  • 783
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42

3 Answers3

1

You're not looking at this correctly. Not all "Icelandic" objects are eligible for garbage collection at this line. And each "Icelandic" object has another object, specifically a Long, which is also eligible for garbage collection, when it's containing "Icelandic" object goes out of scope and/or no longer has a live reference to it.

In this example, the original i2 object, and the Long that "belongs to it" is not eligible for garbage collection until after System.log line.

MobA11y
  • 18,425
  • 3
  • 49
  • 76
1

Assuming that by "at" you mean "exactly before" (as we do with breakpoints).

If you follow the language specification strictly, by my count there are 2 Icelandic objects eligible for garbage collection, each containing one Long, so 4 objects eligible in total.

In practical implementations, however, with all the optimizations that they typically use, lots of other numbers are possible. For example:

  • The JIT might tell the GC that args is never used below that point, so it's eligible before it "officially" goes out of scope (plus 1, then). But with that optimization in place, so would the remaining Icelandic object and its Long (so plus 2 more).
  • A JVM may choose to intern Long objects at an arbitrary range, thus interned values will not be garbage collected (minus 2 or 3, in this case, depending on whether the previous optimization is in place).
  • Any number of other optimizations may be occurring that change the value.

You really cannot know in practice, unless you have a very thorough understanding of the implementation of the executing runtime.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • Or, you could take this for what it is: "A simple interview question dealing with basic inheritance/garbage collecting concepts." Answer 4, and leave the advanced optimization discussion for the face to face part of the interview :) – MobA11y May 20 '13 at 18:09
0

The short answer is the question does not have an answer.

The String[] containing the command-line arguments might become eligible for GC at the end of the call. The Long value might or might not be in a cache internal to Long.valueOf.

Assigning to a local variable which is not subsequently read is not guaranteed to have an effect on GC because assignments may be optimized and variables may be reused due to liveness analysis, and this may change after JIT compilation of code.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245