Releasing memory back to OS when JVM is idle
<p>We have a simple microservice setup, based on Spring Boot and Java 8 on Windows servers.</p>

<p>Many of the services have a low load, since they serve as integrations to all kinds of external partners. So they are idle a lot of the time.</p>

<p>The problem is that the JVM only releases memory back to the OS, when a garbage collection is triggered. So a service might start using 32mb, then serve a single request and allocate 2GB of memory. If there is no other activity on that service, it will not GC and other services on the server will suffer.</p>

<p>Triggering a GC externally or internally with a System.gc works just fine and I have figured out how to use <code>-XX:MaxHeapFreeRatio</code> and <code>-XX:MinHeapFreeRatio</code> with <code>-XX:+UseG1GC</code> to control when the heap should expand and release memory to the OS.</p>

<p><strong>My question is: What is the best way to ensure that memory is relased back to the OS when the JVM is idle?</strong> </p>

<p>One idea would be to have the service monitor itself and trigger a System.gc efter a period of idleness, but that might be tricky and errorprone. So hoping for better suggestions.</p>

<p>You can reproduce by running X instances of this program. About 10 made my Windows machine with 8GB give up. </p>

<pre><code>import java.util.*;

public class Load {
  public static void main(String[] args) throws Exception {
    alloc();
    Scanner s = new Scanner(System.in);
    System.out.println("enter to gc ");
    s.nextLine();
    System.gc();
    System.out.println("enter to exit");
    s.nextLine();
  }

  private static void alloc() {
    ArrayList&lt;String[]&gt; strings = new ArrayList&lt;&gt;();
    int max = 1000000;
    for (int i = 0; i &lt; max; i++) {
      strings.add(new String[500]);
    }
  }
}
</code></pre>

<p><code>c:\&gt; java -server -XX:+UseG1GC -Xms32m -Xmx2048m Load</code></p>

<p>Edit: This was marked as a duplicate two times, but it is not a duplicate of the linked questions. The first question is a 2010 version of the same question, but that question is on why the GC does not release memory back to the OS (which was not possible at that time). The other question is about basic GC settings, that I already wrote that I understand. I wish a discussion of how to trigger the garbage collector when the system is idle. So running System.gc every five seconds is not acceptable, because that would have a high risk of colliding with valid requests and ruin the response times. </p>