-1

When will the data variable here be qualified for garbage collection? And how jvm would know that this data variable has to be alive because my task is alive. The only thing my editor said is the variable needs to be final or effectively final. Ok I won't update it but still how will be the reference carried forwarded? Does java also has concept like closure of javascript or what process is it? I am asking for any official JVM specification documents or any credible source where I can get the information about what exactly happens in this situation.

public void schedule() {
    String data = "adfasdf";
    scheduler.newJob("businessruleid")
             .setCron("0/5 * * * * ?")
             .setTask(executionContext -> {
                System.out.println(executionContext + " " + data);
            }).schedule();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yashdeep Hinge
  • 382
  • 2
  • 14
  • From Java 7 onwards, the Java String Pool is stored in the Heap space, which is garbage collected by the JVM. – Yashdeep Hinge Aug 01 '23 at 06:47
  • https://stackoverflow.com/questions/18406703/when-will-a-string-be-garbage-collected-in-java debatable whether they will be collected or not or when. – Yashdeep Hinge Aug 01 '23 at 07:36

1 Answers1

0

data variable stays alive as long as the task scheduled by scheduler.newJob() is alive because the task's callback function captures data preventing it from being garbage collected.

you can think of the data variable in example as "captured" by the anonymous function you have provided to setTask(). here, "capturing" means that the function retains a reference to data even after schedule() returns.

The anonymous function keeps the data variable alive as long as the function itself is alive. In your case, as long as the scheduled task you have created remains in existence data will not be eligible for garbage collection because it's still being referenced.

JVM knows to keep data alive because it sees that the function has captured data and thus maintains a reference to it. This is not unique to Java; it's a common feature in many programming languages that support closures .

When code runs schedule() the method will complete and under normal circumstances, local variables like data would be eligible for garbage collection when the method returns. But because anonymous function captures data and the function itself is kept alive by the scheduler the JVM knows to keep data in memory.

"final" means that the variable's value cannot change after it's been assigned. It's essentially the same as final, but without the explicit final modifier. It was introduced in Java 8 to give more flexibility in working with lambda expressions and anonymous classes, which can only access final or effectively final variables from an outer scope.

Akhilesh Pandey
  • 855
  • 5
  • 10
  • I understand your answer but if you know about any reference to any official docs or anything like that for java which discuss this same thing capturing. Can you point it please? And Thanks – Yashdeep Hinge Jul 31 '23 at 17:43
  • This is not correct. The variable `data` is only alive for as long as the method is active. The lambda will have a **copy** of the value of `data`, not a reference to `data` itself (though technically that is an implementation detail, I'm not aware of implementations of Java were the capture isn't done by copying). – Mark Rotteveel Aug 01 '23 at 07:05