Nikolay Ivanchev
Nikolay Ivanchev

Reputation: 144

Java GC1 doesn't throw OutOfMemory but keeps struggling

I want my app to fails fast in case of insufficient memory, so it can be adjusted and the process restarted. I am running the service with

-XX:ParallelGCThreads=8

-XX:GCTimeRatio=19

-XX:ConcGCThreads=2

When heap memory runs out, GC1 keeps doing Full Garbage Sweeps, which are STW (Stop the World) events. It fights bravely but futile. jstat -gnew shows that for 10 consecutive FGC attempts it failed to secure more then 0.03% of Olg Den space

I looked into GC1 docs but I cannot find a suitable options to instruct GC1 to throw OutOfMemory when it cannot reclaim more then 0.05 % going from 100 in lets say 5 FGC attempts. Any ideas? Thanks

Upvotes: 3

Views: 220

Answers (1)

jpseng
jpseng

Reputation: 2210

It is not possible to configure any thresholds for when an OutOfMemoryException should be thrown.

Unfortunately, a garbage collection panic with long stop the world pauses is typical when memory is running low before finally an OutOfMemoryException is thrown by the JVM.

In my optimisations of the G1GC, the option -XX:+UseStringDeduplication has proven to be very valuable. The effect depends on how many strings the application creates, for example caused by marshalling and unmarshalling.

What also helped a bit to reduce the number of long GC breaks was to adopt the region size (-XX:G1HeapRegionSize). Especially if the GC logs frequently show 'to-space exhausted' before a stop-the-world event, it is worth adjusting this option.

From my experience, the best optimisations for less annoyingly long GC pauses are:

  • add more RAM to the JVM ;-)
  • less memory consumption of the application (do profiling sessions to see the memory consumption hot spots)
  • try newer Java version with other garbage collectors (Shenandoah, Z1).
  • and of course detect memory leaks in the application

Upvotes: 1

Related Questions