Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection and asynchronous calls / Future objects

Below is a sample code that utilizes the Future interface to make an asynchronous call. I need some clarification about 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 a method , so the variable will be soon cleared by the GC after the method's execution as it is no longer used. So in case that the code enters the if statement , what will be the state of the JVM? How is the JVM going to handle the wrapped result in case that noone is going to read it back? Does it affect the Thread Pool , or the thread Executor?

like image 644
AntJavaDev Avatar asked Jun 08 '15 10:06

AntJavaDev


People also ask

Which objects are eligible for garbage collection?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.

What is the main objective of garbage collection?

The main objective of Garbage Collection is to free heap memory by destroying the objects that don't contain a reference. When there are no references to an object, it is assumed to be dead and no longer needed. So the memory occupied by the object can be reclaimed.

What is garbage collection in object oriented programming?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

How can we prevent objects from garbage collection?

By Increasing Heap Memory This approach leads to the garbage collector running infrequently, however when it runs, will take longer than before to complete the garbage collection task.


1 Answers

How is the JVM going to handle the wrapped result in case that noone is going to read it back?

Presumably you got the Future object from an Executor. For this executor to be able to set the result in the Future, it holds a reference to the Future. In other words, just because the method local reference to the object disappears as the call stack is popped, doesn't mean that the Future object (which is on the heap) is automatically eligible for garbage collection.

The async call is not cancelled or anything like that. The executor will perform the call, fill in the result, and presumably drop it's reference to the Future object. At this point the object becomes unreachable and eligible for garbage collection.

If you're certain that your code doesn't keep a reference to the Future object (i.e. leaking it in the // do something... part) then you can be sure that the Future object is (eventually) collected by the GC. (The executor doesn't have any subtle memory leaks here.)

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

To be precise, the variable will be discarded as the call stack is popped. This will eventually cause the Future object to be unreachable and eligible for garbage collection. The object will however typically not be garbage collected immediately as the method returns.

like image 131
aioobe Avatar answered Oct 17 '22 19:10

aioobe