Search code examples
javagarbage-collectionheap-memory

Java GC on low memory stop and crash program


I am using parallel GC in my java program. But when jvm have less then ~400mb of free memory GC starts to consume all of CPU. And after some time jvm crash with java.lang.OutOfMemoryError: GC overhead limit exceeded. But i am printing the amount of free memory with Runtime.getRuntime().freeMemory(), and i am sure that jvm have ~400mb of free space. I tried to set -XX:MinHeapFreeRatio to 1 or 5 but it made everything worst.

How to config GC or which one should i choose to prevent it from consumimg all of CPU and crashing jvm? My jvm flags is -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Xmx3800m -server -Djava.library.path="native_libs"


Solution

  • My app have a lot stuff in old gen, so how i understood GC was trying to free enough memory for young gen and have been making full gc very often. So by reducing Young gen size i extend size of old gen, which was heavily used by me.

    To reduce Young gen size you can use -XX:MaxNewSize, -XX:NewRatio, -XX:NewSize. I used -XX:MaxNewSize. You can read about it here https://docs.oracle.com/cd/E19900-01/819-4742/abeik/index.html