I am using GC options XX:+UseParNewGC-XX:+UseConcMarkSweepGC
for my application.
As most of you are already experiencing JVM is good at increasing heap up to max heap size, however it does not release memory back to OS. I came across -XX:MaxHeapFreeRatio
and -XX:MinHeapFreeRatio
but these are ignored for Parallel Garbage Collectors.
Are there special options to force JVM release memory back to OS for -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio
combination.
The JVM does release back memory under some circumstances, but (for performance reasons) this does not happen whenever some memory is garbage collected. It also depends on the JVM, OS, garbage collector etc. You can watch the memory consumption of your app with JConsole, VisualVM or another profiler.
When a Java program needs memory, it requests this memory from the JVM. If there is no memory left, then the JVM will attempt to free some memory by using the garbage collector. The garbage collector will try to release memory that is no longer required to run the program back to the JVM.
Java has an automatic memory deallocation system known as Garbage Collector. This article presents you with some concepts of Java Memory management, working of Garbage Collector in JVM.
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
On my Java 1.8.0_45 -XX:+UseG1GC makes memory shrink. This is my test:
MemoryPoolMXBean m = ManagementFactory.getMemoryPoolMXBeans().get(5);
System.out.println(m.getName());
byte[] a = new byte[512 * 1024 * 1024];
System.out.println(m.getUsage().getCommitted() / 1024 / 1024);
a = null;
System.gc();
Thread.sleep(1000);
System.out.println(m.getUsage().getCommitted() / 1024 / 1024);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With