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.
aioobe
source share