Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JVM release memory back to OS [duplicate]

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.

like image 311
Pushkar Avatar asked May 05 '15 12:05

Pushkar


People also ask

Does JVM return memory to OS?

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.

How do I free up JVM memory?

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.

Does Java automatically deallocate memory?

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.

How does JVM manage memory?

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.


1 Answers

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);
like image 200
Evgeniy Dorofeev Avatar answered Sep 24 '22 13:09

Evgeniy Dorofeev