I got two conflicting answers regarding this question from two places.
- The baeldung article says
For example, Parallel GC doesn’t release unused memory back to the OS readily.
So I think Parallel GC will not shrink the heap size.
- The so thread says
you can make the GC more aggressive in general by setting -XX:GCTimeRatio=19 -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=30 which will allow it to spend more CPU time on collecting and constrain the amount of allocated-but-unused heap memory after a GC cycle.
So I think I can force the garbage collector to shrink the heap size.
Based on the above understanding, I do experiments on my machine.
3.1 The jdk:
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)3.2 I use
jmap -heapcommand to print the GC information, the output is as below.....(omit other infomration)
using thread-local object allocation.
Parallel GC with 8 thread(s)3.3 I run an application with VM options
-Xmx10240m(it is the only heap-related VM option I set manually), and openjvisualvmto observe the change of heap size. I notice the heap size(the orange part) shrinks when the used heap size(the blue part) becomes smaller. It violates the first article.
3.4 I run the same application with VM options
-Xmx10240m -XX: MaxHeapFreeRatio=100, I set the option-XX:MaxHeapFreeRatio=100in order to tell GC not to shrink. But it does not work either.
My concrete question is:
4.1 The baeldung article says Parallel GC doesn’t release unused memory, so why does the heap size in Figure 1 still shrink?
4.2 The so thread says the -XX:MaxHeapFreeRatio will make some differences, but it didn't.
Any help will be appraciated.
In this bug report about releasing memory, there is a ten year old comment from Jesper Wilhelmsson:
I would trust the JDK developers more than articles on baeldung.com or rather, I’d treat articles on baeldung.com with a grain of salt in general. There is no sign of a quality assurance process on that site and it’s not the first time, incorrect information has been found on that site.