title: Why does java wait so long to run the garbage collector?
body: <p>I am building a Java web app, using the <a href="http://www.playframework.org/" rel="nofollow noreferrer">Play! Framework</a>.  I'm hosting it on <a href="http://www.playapps.net/" rel="nofollow noreferrer">playapps.net</a>.  I have been puzzling for a while over the provided graphs of memory consumption.  Here is a sample:</p>
  
  <p><img src="https://i.stack.imgur.com/2Y4GX.png" alt="Heap Memory"></p>
  
  <p>The graph comes from a period of consistent but nominal activity.  I did nothing to trigger the falloff in memory, so I presume this occurred because the garbage collector ran as it has almost reached its allowable memory consumption.</p>
  
  <p><strong>My questions:</strong>  </p>
  
  <ul>
  <li>Is it fair for me to assume that my application does <em>not</em> have a memory leak, as it appears that all the memory is correctly reclaimed by the garbage collector when it does run?  </li>
  <li>(from the title) Why is java waiting until the last possible second to run the garbage collector?  I am seeing significant performance degradation as the memory consumption grows to the top fourth of the graph.</li>
  <li>If my assertions above are correct, then how can I go about fixing this issue?  The other posts I have read on SO seem opposed to calls to <code>System.gc()</code>, ranging from neutral ("it's only a request to run GC, so the JVM may just ignore you") to outright opposed ("code that relies on <code>System.gc()</code> is fundamentally broken").  Or am I off base here, and I should be looking for defects in my own code that is causing this behavior and intermittent performance loss?  </li>
  </ul>
  
  <p><strong>UPDATE</strong><br>
  I have opened a discussion on PlayApps.net pointing to this question and mentioning some of the points here; specifically @Affe's comment regarding the settings for a full GC being set very conservatively, and @G_H's comment about settings for the initial and max heap size. </p>
  
  <p>Here's a <a href="http://help.playapps.net/discussions/questions/49-is-there-anything-non-standard-about-the-garbage-collection-used-by-the-jvm-on-playapps" rel="nofollow noreferrer">link to the discussion</a>, though you unfortunately need a playapps account to view it.</p>
  
  <p>I will report the feedback here when I get it; thanks so much everyone for your answers, I've already learned a great deal from them!</p>
  
  <p><strong>Resolution</strong><br>
  Playapps support, which is still great, didn't have many suggestions for me, their only thought being that if I was using the cache extensively this may be keeping objects alive longer than need be, but that isn't the case.  I still learned a ton (woo hoo!), and I gave @Ryan Amos the green check as I took his suggestion of calling <code>System.gc()</code> every half day, which for now is working fine. </p>