Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tune Java GC, so that it would immediately throw OOME, rather than slow down indeterminately

I've noticed, that sometimes, when memory is nearly exhausted, the GC is trying to complete at any price of performance (causes nearly freeze of the program, sometimes multiple minutes), rather that just throw an OOME (OutOfMemoryError) immediately.

Is there a way to tune the GC concerning this aspect?

Slowing down the program to nearly zero-speed makes it unresponsive. In certain cases it would be better to have a response "I'm dead" rather than no response at all.

like image 305
java.is.for.desktop Avatar asked Jul 15 '11 10:07

java.is.for.desktop


2 Answers

Something like what you're after is built into recent JVMs.

If you:

  • are using Hotspot VM from (at least) Java 6
  • are using the Parallel or Concurrent garbage collectors
  • have the option UseGCOverheadLimit enabled (it's on by default with those collectors, so more specifically if you haven't disabled it)

then you will get an OOM before actually running out of memory: if more than 98% of recent time has been spent in GC for recovery of <2% of the heap size, you'll get a preemptive OOM.

Tuning these parameters (the 98% in particular) sounds like it would be useful to you, however there is no way as far as I'm aware to tune those thresholds.

However, check that you qualify under the three points above; if you're not using those collectors with that flag, this may help your situation.

It's worth reading the HotSpot JVM tuning guide, which can be a big help with this stuff.

like image 113
Cowan Avatar answered Sep 20 '22 02:09

Cowan


I am not aware of any way to configure the Java garbage collector in the manner you describe.

One way might be for your application to proactively monitor the amount of free memory, e.g. using Runtime.freeMemory(), and declare the "I'm dead" condition if that drops below a certain threshold and can't be rectified with a forced garbage collection cycle.

The idea is to pick the value for the threshold that's large enough for the process to never get into the situation you describe.

like image 30
NPE Avatar answered Sep 19 '22 02:09

NPE