0

I have a Spring Boot application in which everytime API call is made, I am creating an ExecutorService with fixedThreadPool size of 5 threads and passing around 500 tasks to CompletableFuture to run Async. I am using this for a migration of lakhs of data.

As I started the migration, initially API was working fine and each API Call ( Basically code logic + ThreadPool Creation + Jobs Assignment to threads ) was taking around just 200 ms or so. But as API calls increased and new threadpools kept on creating, I can see gradual increase in time being taken to Create the thread Pool and assign the jobs, as a result API response time went till 4 secs. Note : After the jobs are done, i am shutting down the executor service in finally block.

Question :

  1. Can multiple creation create overhead to the application and do those pools keep on piling up?
  2. Wont there be any automatic garbage collection to this ?
  3. Will there be any limit to how many pools get created ?
  4. And what could be causing this time delay ..

I can add further clarifications based on specific queries..

Suhas
  • 55
  • 9

1 Answers1

2

Can multiple creation create overhead to the application and do those pools keep on piling up?

Yes absolutely. Unless you shutdown the thread pools, they won't be destroyed automatically and consume resources. See next question for more details.

Wont there be any automatic garbage collection to this ?

You need to take care that the thread pools are destructed after they are no longer needed. For example, the javadoc of ThreadPoolExecutor provides some hints:

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

Will there be any limit to how many pools get created ?

There is no hard limit on how many threads are supported by Java, however there may be restrictions depending on your operating system and available resources such as memory. This is quite a complex question, more details can be found in the answers to this question: How many threads can a Java VM support?

And what could be causing this time delay?

I assume that you don't have a proper cleanup / shutdown mechanism in place for the thread pools. Every thread allocates at least 1 MB of memory for the thread stack. For example, the more threads you create, the more memory your application consumes. Depending on the system / jvm configuration, the application may utilize swap which dramatically slows down the performance. There may be other things that cause a drop in performance, so this is just what came to my mind right now.

Profilers will help you to identify performance issues or resource leaks. This article by Baeldung shows a few profilers you could use.

Markus
  • 1,649
  • 1
  • 22
  • 41