How to make JVM release memory back to OS [duplicate]
Asked Answered
N

1

10

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.

Nessa answered 5/5, 2015 at 12:29 Comment(3)
Well.. Even if you find options for these things, they are not guaranteed to work across architectures.Slotnick
@Slotnick - Are -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio guaranteed to work on Oracle's 64 bit linux JVM I am not too sure if above parameters -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio are only for serial garbage collectors or not. I am finding contradictory citations for that.Nessa
XX:+UseParNewGC with the max and min heap free ratios does release memory back to the OS in java 1.8.0_73 onwards for my Windows 64bit setup at least. If you don't explicitly ask it to GC, however, it might only decide to do so if the OS is under memory pressure. Finally, I do not know if the UseConcMarkSweepGC option changes any of this.Devisal
M
8

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);
Methinks answered 5/5, 2015 at 12:51 Comment(2)
Do I need to tweak any parameters other than switching garbage collector to G1GC?Nessa
If G1GC enables shrkinging then -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio should work tooMethinks

© 2022 - 2024 — McMap. All rights reserved.