Monday, February 08, 2021

Is there a way to disable garbage collection

Not until recently did I find that Java 11 has introduced a no-op garbage collector, which essentially does nothing. Configuring the Java runtime with this garbage collector essentially disables garbage collection.

Also to be noted is the fact that until Java 11, there is no way to disable garbage collection. That makes sense as for someone like me who have been using Java for the last 20 years, since version 1.2, I was never faced with a situation where there was a need to disable GC. Java does not have an option to deallocate memory. If GC is disabled, who will free up the memory that got allocated through new object creations?

So why now was I looking to disable garbage collection now?

Well, we were into micro-services implementation and experimenting with GraalVM and micro-services frameworks.

Our objective is to spin up services as quickly as possible and replace a running instance when any of our monitored parameters starts showing any sign of weakness.

The obvious problem with Java garbage collection showed its head in our deliberations. If Java stops everything when it does garbage collection, why not 

  • Disable the garbage collection
  • Stop the instance when memory usage goes beyond a threshold
  • Spin up a new instance to handle the low

Sounded by a good option to try

EpsilonGC is the no-ops GC introduced with Java 11. This can be enabled by starting the runtime with configuration flags 

-XX:+UseEpsilonGC

and

-XX:+UnlockExperimentalVMOptions 

Turned out that for our case, having proper GC configured on running instance is a better configuration to have, then to kill it and spin up a new instances frequently. But we are keeping the option of using EpsilonGC open to use when we find a right fit use case.  

We did quiet a few experiments with EpsilonGC. More on that hereherehere & here

Complete EpsilonGC specification can be found at JEP-318


No comments:

Post a Comment