4

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.

Eugene
  • 117,005
  • 15
  • 201
  • 306
Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
  • so your question is _only_ before java-12, I guess? – Eugene Mar 12 '20 at 10:29
  • @Eugene no, the question is for, say, Java 8, 11, 13, just the answer may differ for versions. – Piotr Findeisen Mar 13 '20 at 11:30
  • I've updated my answer with an extensive reference, if that matters. but the bottom line is simple : that flag _does_ matter, to which extent: depends on the exact VM version, initial flags that are given to the VM. – Eugene Mar 18 '20 at 18:15

2 Answers2

0

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)

Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
0

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.

Eugene
  • 117,005
  • 15
  • 201
  • 306