Does Java G1 garbage collector respect MaxHeapFreeRatio parameter?

Viewed 888

Does the Java G1 garbage collector (as implemented in Open JDK) respect the -XX:MaxHeapFreeRatio=n JVM parameter?

Does it respect it in Java 8?

I found JEP 346: Promptly Return Unused Committed Memory from G1 delivered in Java 12, but it's not clear to me what was the state before it.

2 Answers

A non-authoritative answer that I found is based on https://bugs.openjdk.java.net/browse/JDK-8078039?focusedCommentId=13632717&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-13632717

Most notably, some GCs support it, some do not. This RFE doesn't say which GC is used, so I assume we are talking about the default GC which supports MaxHeapFreeRatio. G1 also supports this option.

To shrink the heap (and release memory) a full GC is required. It the application doesn't trigger a full GC manually, it may take a while before one is triggered by the JVM.

So the answer would be (for Java 8): yes... but no.

  • yes, it supports the option
  • but only when full GC happens, which may be never

(I remain curious whether there is a more authoritative source and what's the current state, in more modern JVMs)

I do not know a more authoritative answer than the source code and yes, you are correct in your answer - the release of the memory will happen only after a Full GC (at least before that JEP).

For java-8:

That argument does matter, look here for example.

That code is not very complicated to understand, and here is the actual shrink that happens.

For a much more extensive answer (regarding java-11, but still qualifies to java-8), read this.

The bottom-line is that - that flag does matter, but how exactly is implementation dependent. There is no simple answer to your question.

Related