Setting -XX:+DisableExplicitGC in production: what could go wrong?

garbage-collection, java, tomcat

Solution

You are not alone in fixing stop-the-world GC events by setting the `-XX:+DisableExplicitGC` flag. Unfortunately (and in spite of the disclaimers in the documentation), many developers decide they know better than the JVM when to collect memory and introduce exactly this type of issue.

I'm aware of many instances where the `-XX:+DisableExplicitGC` improved the production environment and zero instances where there were any negative side effects.

The safe thing to do is to run your current production code, under load, with that flag set in a stress test environment and perform a normal QA cycle.

If you cannot do that, I would suggest that the risk of setting the flag is less than the cost of not setting it in most cases.

Problem

we just had a meeting to address some performance issues in a web application that is used to calculate insurance rates. The calculations are implemented in a C/C++-module, that is used in other software packages as well. To make it available as a webservice, a Java wrapper was implemented that exposes an XML based interface and calls the C/C++-module via JNI. Measurements showed that several seconds were spent on each calculation inside the Java part. So my first recomodation was to enable garbage collection logging in the VM. We could see at once that many stop-the-world full GCs were made. Talking about that, the developper of the java part told us they did a `System.gc()` on several occasions "to make sure the memory is released after use". OK, I won't elaborate on that statement any further... ;-) We then added abovementioned `-XX:+DisableExplicitGC` too the VMs arguments and reran the tests. This gained about 5 seconds per calculation. Since we cannot change the code by stripping all those `System.gc()` calls at this point in our release process, we are thinking about adding `-XX:+DisableExplicitGC` in production until a new Jar can be created. Now the question is: could there be any risk in doing so? About the only thing I can think of is tomcat using `System.gc()` internally when redeploying, but that's just a guess. Are there any other hazards ahead?

Original source

Related problems