0

Let's say I have this peace of code:

public static void someMethod(){
    
    int sum = 0;
    {

        int a = 3;
        int b = 4;
        sum = a + b;

    }

    //a and b is not visible here
    //but a and b will be eligible for GC?

    //more code

}

My question is if a and b are eligible for GC after execution exit from that block or only after the someMethod is done.

KunLun
  • 3,109
  • 3
  • 18
  • 65
  • In theory, yes. In actuality, the (JIT) compiler will likely remove `a` and `b` entirely. – BeUndead Oct 07 '20 at 15:12
  • @BeUndead, thank you. I like to use a lot of this blocks for readability and I don't want to badly influence my code. Of course, values of `a` and `b` came from other methods. In question is just a simple example of block.. – KunLun Oct 07 '20 at 15:30
  • [here is a rather famous question about it](https://stackoverflow.com/questions/24376768/can-java-finalize-an-object-when-it-is-still-in-scope) – Eugene Oct 07 '20 at 17:21

1 Answers1

1

In theory any local variable ceases to be a GC root as soon as it goes out of scope. (Even if there is non-local access to the variable via a nested class, etc. In that case, the value in the variable will have copied into another (synthetic) variable with a longer lifetime.)

In practice, the JVM may actually wait until the enclosing method call returns. It is implementation dependent.

But it your example:

  1. Those local variables could be optimized away by the JIT compiler.
  2. They are primitive variables, so they don't hold references to objects anyway. The GC won't pay any attention to them.

In general, you wouldn't normally worry about this. It rarely matters that a few variables become unreachable a bit later than is strictly necessary.

However consider the following:

public static void longRunningMethod() {
    // do stuff
    {
       Map<X, Y> bigMap = ... // create and populate a huge map
       // use bigMap
       bigMap = null;
    }
    // long running computation
}

If you had clear evidence of a "garbage retention" problem due to bigMap, you might consider assigning null to it, as above. On the other hand, that assignment could also be a candidate for being optimized away!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • That block was just a simple example of block. Normally, I compute the value of a needed variable(in majority cases are objects) by calling some methods. And I like to use blocks like that for readability and I want to be sure I don't badly influence my code. And when GC efectivly do his job, of course, dependes by implementation. Thank you. – KunLun Oct 07 '20 at 15:34
  • @KunLun [here is one where this matters](https://stackoverflow.com/questions/58714980/rejectedexecutionexception-inside-single-executor-service) – Eugene Oct 07 '20 at 17:21
  • 2
    Garbage collection is about objects, not local variables. Mixing these things up, will inevitably lead to half-truths and imprecise statements. The scope of variables only determines the validity of names, not the reachability of objects. An object is eligible to garbage collection when the program can proceed without ever touching the object’s memory again. – Holger Oct 08 '20 at 12:08