1
votes

I was going through the concepts of Garbage collection in Java. From what I learnt, it says that the GC runs periodically in the background and checks for any un-referenced or unreachable objects. But when there is some sort of pressure on the memory, then only the GC is done. Also GC has phases like Marking phase, Relocating phase etc..In the following program, at the end of line 1, are the objects eligible for garbage collection as there is no guarantee that GC will always run or they are marked for GC immediately after Line 1?

Correct me if i'm wrong at any of my statements.

public class ImmutableStrings
{
    public static void main(String[] args)
    {
        String one = "someString";
        String two = new String("someString");
        String three = "someString";
        one = two = three=null;
        System.out.println("testing "); // 1
        System.out.println("testing again");// 2
    }
}

At line no 1 , how many objects are marked for garbage collection? According to me, 2 objects will be eligible for GC and none marked. Answers may vary

1
From my understanding the Objects in memory have become eligible for garbage collection, however, will still occupy space until the garbage collection 'mark and sweep' algorithm is executed. I assume that, by chance, the GC could run the moment after you mark the variables one, two and three to null so that in line 1 so you may have multiple answers to your final question. - Zachary
Note that the behaviour of the garbage collector is intentionally defined very vaguely, so that the various implementations have very few restrictions set on them. This is also why System.gc() does not necessarily do anything. - Thorbjørn Ravn Andersen

1 Answers

2
votes

No objects are marked for garbage collection until the GC actually runs. And the marks are made by the garbage collector and are only valid / meaningful while the GC is running.

So asking whether objects are marked at a particular point in the code is ... meaningless.

You might be trying to make the distinction between objects that are unreachable versus objects that the GC will actually collect. But even that is typically unanswerable because different Java GCs behave differently in terms of when objects are collected.


As for what is eligible for garbage collection, the count depends on the JVM / GC implementation, and how you count the objects. For example:

  • The String object created by the explicit new will (most likely) be unreachable.

  • The String objects corresponding to the string literals will probably NOT be unreachable. (Unless the GC is smart enough to realize that the main method cannot use them again .... which is unlikely)

  • But ... the first String object actually has an char[] inside it.

  • And ... the System.out.println("testing ") call may generate garbage.

  • And ... there could be any number of other objects created by the JVM (while instantiating the JVM, building the args array, loading classes, etc, etc) that could be unreachable at that point.


In short, the question as posed by the quiz is too vague for a definitely correct answer.