3

I am writing some code where I might need to create an unbounded number of future objects (java.util.concurrent.Future).

But I am worried about running out of memory at some point.

Couple of questions here:

  1. Does the jvm know that once the future has completed, it is not being refernced anywhere and therefore is eligible for GC (even if the thread within which it was created is still alive and running)?
  2. Ideally, I wouldn't want to keep track of these futures themselves. But if I do keep the reference of these futures and periodically call cancel on them, will that make them available for GC?
Shri Javadekar
  • 258
  • 3
  • 11

2 Answers2

6

You'll need to eventually remove any references to the Futures in order for them to be garbage collected. The usual practice is to maintain a collection of the Futures and periodically check whether isDone() returns true. If so, the task has completed and references to it may be cleaned up. If you are worried about piling up some long-running tasks which may be safely interrupted, you need to both call cancel() on the Future and remove/null out any references to it which may exist.

In general, it is always a bad idea to architect a system which can potentially experience unbounded growth. If the number of outstanding Future objects grows too large, you should apply back pressure elsewhere in the system.

It is not necessarily the case that "once the future has completed, it is not being referenced anywhere." For example, a client with reference to it could request the result via the get() method at any point. The JVM therefore needs to keep the Future live until all these external references have been removed. The references within the thread pool will be removed when the Future is "done" (meaning either it completed its task or was cancelled).

Andrew Bissell
  • 2,827
  • 2
  • 14
  • 19
  • Thanks! And ExecutorService is passed to my code and I create futures by calling `submit()` on that ExecutorService. That executorService will continue to hold the reference to the futures even when they complete. And I don't think there is a way to tell the executorService to purge the completed futures :(. – Shri Javadekar Nov 22 '13 at 06:45
  • The `ExecutorService` will *not* hold onto references to tasks submitted through `submit()` once they complete. You only need to be concerned about this if there's some reference to the `Future` elsewhere in your running program. – Andrew Bissell Nov 22 '13 at 09:01
  • No problem. Please remember to accept an answer with the check-mark if it addressed your issue. :) – Andrew Bissell Nov 22 '13 at 20:25
0

Have you seen this question?

Once the computation of a Future is complete, you cannot cancel it anymore. I don't know the exact context, but one suggestion would be to keep track of the futures, cancel the ones you want or need to cancel and call purge on the executor to remove them from the working queue.

Hope it helps.

Community
  • 1
  • 1
Diana Sule
  • 61
  • 3
  • Thanks for pointing to this. However, I'm using an executorService and not a ThreadPoolExecutor. And ExecutorService doesn't seem to have a `purge()` method. – Shri Javadekar Nov 22 '13 at 07:24