9

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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Pushkar
  • 541
  • 4
  • 18
  • Well.. Even if you find options for these things, they are not guaranteed to work across architectures. – TheLostMind May 05 '15 at 12:30
  • @TheLostMind - 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. – Pushkar May 05 '15 at 16:17
  • 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. – nsandersen Aug 30 '17 at 11:32

1 Answers1

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);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275