title: Reduce Unused Heap size in JVM
body: <p>This has been asked similarly <a href="https://stackoverflow.com/questions/4952568/is-there-a-way-to-lower-java-heap-when-not-in-use">here</a> and <a href="https://stackoverflow.com/questions/22480749/how-to-shrink-java-heap-space?noredirect=1&amp;lq=1">here</a> but neither changes to the JVM parameters has lead me to the same solution. I simply want to reduce my JVM's unused heap size, so that it more closely reflects actual usage (freeing it to the OS). I've been using YourKit to profile the memory usage and it typically looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/MNxhg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MNxhg.png" alt="enter image description here"></a></p>
  
  <p>I'm running intellIj IDEA Community Edition 64-bit, on Windows 7 64-bit, 8 GB RAM. I've edited the <code>.vmoptions</code> file found here: <code>C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 14.1.5\bin\idea64.exe.vmoptions</code> with the suggestions mentioned in the links above. The first link's answer is actually wrong/out-dated, but 2 comments link to articles found <a href="http://stopcoding.wordpress.com/2010/04/12/more-on-the-incredible-shrinking-jvm-heap" rel="nofollow noreferrer">here</a> and <a href="http://www.stefankrause.net/wp/?p=14" rel="nofollow noreferrer">here</a> which seem promising. The thing is, I've tried all of the different Garbage Collectors mentioned, and still see a similar (not identical) heap size shown above.</p>
  
  <p>How do I reduce allocated heap size (olive green) to more accurately reflect the actual usage (blue)? Shouldn't Garbage Collection lower the heap size, not increase it like currently.</p>
  
  <p><strong>I've tried configuring my idea64.exe.vmoptions as follows:</strong></p>
  
  <p>As per the last link above, it claims </p>
  
  <blockquote>
    <p>Only the Serial GC and G1 release unused memory to the OS</p>
  </blockquote>
  
  <p>It even visually shows how the heap is reduced. Yet I've had no luck and have tried all of the following: What's going on??</p>
  
  <p><code>-XX:+UseSerialGC</code></p>
  
  <p>and</p>
  
  <p><code>-XX:-UseSerialGC</code></p>
  
  <p>and</p>
  
  <pre><code>-XX:+UseG1GC
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-XX:-UseG1GC
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Both of the above together, with and without ("+/-")
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-XX:-UseAdaptiveSizePolicy
  -XX:-UseParallelGC
  </code></pre>
  
  <p>and</p>
  
  <p><code>-XX:UseConcMarkSweepGC</code></p>
  
  <p>and</p>
  
  <pre><code>-Xms128m
  -Xmx750m
  -XX:MaxPermSize=350m
  -XX:ReservedCodeCacheSize=225m
  -XX:+UseG1GC
  -XX:SoftRefLRUPolicyMSPerMB=50
  -ea
  -Dsun.io.useCanonCaches=false
  -Djava.net.preferIPv4Stack=true
  -Djsse.enableSNIExtension=false
  </code></pre>
  
  <p>and </p>
  
  <pre><code>-server
  -Xms128m
  -Xmx512m
  -XX:MaxPermSize=250m
  -XX:ReservedCodeCacheSize=150m
  -XX:UseConcMarkSweepGC
  -XX:SoftRefLRUPolicyMSPerMB=50
  -ea
  -Dsun.io.useCanonCaches=false
  -Djava.net.preferIPv4Stack=true
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>To reiterate why this isn't a duplicate, the other "answer" is 5 years old and fails to even mention any of the modern Garbage Collectors. The only helpful parts are the two links in the comments which point to different JVM parameters to use, all of which I've tried and don't have success with. I've tried those solutions in full, and still have no result. This is precisely why I'm urging you to consider this as unique, as others are likely experiencing the same circular frustration.</p>