Punter Vicky
Punter Vicky

Reputation: 16982

ExecutorService Shutdown - Kafka

I am fetching messages from Kafka and processing them using Executor service similar to below. I am not calling executorService.shutdown(). I have occasionally been seeing heapSize exception , but not sure if this could be one of the causes. How do the unused Runnable instances get removed after completion? Should I do anything specific to explicitly make it eligible for GC?

public class Consumer implements CommandLineRunner{

    ExecutorService executorService;
    executorService = Executors.newFixedThreadPool(50)
    executorService.submit(runnable);

  }
}

Upvotes: 2

Views: 1094

Answers (1)

stjepano
stjepano

Reputation: 1152

From documentation for Executors.newFixedThreadPool:

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

For given example there will be at most 50 threads active in the thread pool and they are released when you call shutdown method.

If you do not keep references to your Runnables they will be GCed when Executor is done with them. If you get out of memory exceptions this can be due to queued Runnables in cases when executor can not keep up with the work submitted to him.

EDIT: Also out of memory exceptions can happen if your tasks take a lot of memory (obviously).

Upvotes: 1

Related Questions