Garbage Collection and Asynchronous Calls / Future Objects

The following is sample code that uses the Future interface for an asynchronous call. I need to clarify the get () method.

Future<String> future = getAsyncString(); //do something ... String msg = ""; if (validation) return; else msg = future.get(); //do something else... return; 

The future variable is initialized in the method, so the variable will soon be cleared by the GC after the method is executed, since it is no longer used. So if the code is in an if statement, what will be the state of the JVM? How is the JVM going to handle the wrapped result if no one is going to read it? Does this affect the thread pool or Executor thread?

+7
java multithreading
source share
4 answers

How will the JVM handle wrapped results if no one is going to read it?

If no one (I mean any program) reads it, then the GC will take care of this during the garbage collection. But this does not mean that getAsyncString() will not be fully executed; instead, it will complete normally when the normal method is completed.

+2
source share

How will the JVM handle wrapped results if no one is going to read it?

Presumably, you got a Future object from Executor . So that this artist can set the result in Future , it contains a link to Future . In other words, just because the local method reference to the object disappears as the call stack is called does not mean that the Future object (which is on the heap) is automatically suitable for garbage collection.

An asynchronous call is not canceled or something like that. The contractor will make the call, fill in the result, and supposedly drop the reference to the Future object. At this point, the object becomes inaccessible and has the right to garbage collection.

If you are sure that your code does not support a reference to a Future object (i.e., leaking it in the // do something... ), you can be sure that the Future object is (eventually) compiled by the GC. (The artist has no memory leaks.)

[...] so the variable will be cleared by GC soon.

To be precise, the variable will be dropped as the call stack pops up. This will ultimately lead to the Future object becoming unavailable and will have the right to garbage collection. However, the object, as a rule, will not collect garbage immediately after the method returns.

+10
source share

I think. The planned future will have some internal links from threadpools queues until the task is completed. Therefore, it cannot be built by gc until the task is completed.

Maybe there is an additional level of abstraction between the future and the performer, and the future can be gathered. But I am sure that if the task is sent, it will be launched. It does not matter if the pointer to the future has been saved or not.

+1
source share

You have a guarantee that the object will not be GCed while you are in the area in which the link to it is indicated, or there is a link to the object somewhere in the code.

This applies to all objects, and the future does not matter here.

So, as soon as your method completes and its call stack is cleared, at some point in the future your object will have the right to garbage collection, but, of course, not before the link to it exists in the method call stack.

0
source share

All Articles