Erel Segal-Halevi Erel Segal-Halevi - 4 months ago 19
Java Question

Running garbage-collection in the idle time of a Java web server

We have a Java web server that often decides to do garbage-collection while it is running a service. We would like to tell it to do garbage-collection in the idle time, while no service is running. How can we do this?

Answer Source

You would need to be able to find out when the web container is idle, and that is likely to depend on the web container that you web container that are using.

But I think that this is a bad idea. The way to force the GC to run is to call System.gc(). If that does anything (!) it will typically trigger a major garbage collection, and will likely take a long time (depending on the GC algorithm). Furthermore, the manually triggered collection will happen whether you need to run the GC or not1. And any request that arrives when the GC is running will be blocked.

In general, it is better to let the JVM decide when to run the GC. If you do this, the GC will run when it is efficient to do so, and will mostly run fast young space collections.

If you are concerned with request delays / jitter caused by long GC pauses, a better strategy is to tune the GC to minimize the pause times. (But beware: the low-pause collectors have greater overheads compared to the throughput collectors. This means that if your system is already heavily loaded a lot of the time, then this is liable to increase average response times.)

Other things to consider include:

  • Tuning your application to reduce its rate of generating garbage.
  • Tuning your application to reduce its heap working-set. For example, you might reduce in-memory caching by the application.
  • Tuning the web container. For example, check that you don't have too many worker threads.

1 - The best time to run the GC is when there is a lot of collectable garbage. Unfortunately, it is difficult for application code to know when that is. The JVM (on the other hand) has ways to keep track of how much free space there is, and when it is a good time to collect. The answer is not always when the heap is full.