title: Detecting memory-leak programmatically
body: <p>If, on purpose, I create an application that crunches data while suffering from memory-leaks, I can notice that the memory as reported by, say:</p>
  
  <pre><code>Runtime.getRuntime().freeMemory()
  </code></pre>
  
  <p>starts oscillating between 1 and 2 MB of free memory.</p>
  
  <p>The application then enters a loop that goes like this: GC, processing some data, GC, etc. but because the GC happens so often, the application basically isn't doing much else anymore. Even the GUI takes age to respond (and, no, I'm not talking about EDT issues here, it's really the VM basically stuck in some endless GC'ing mode).</p>
  
  <p>And I was wondering: is there a way to programmatically detect that the JVM doesn't have enough memory anymore?</p>
  
  <p>Note that I'm not talking about ouf-of-memory errors nor about detecting the memory leak itself.</p>
  
  <p>I'm talking about detecting that an application is running so low on memory that it is basically calling the GC all the time, leaving hardly any time to do something else (in my hypothetical example: crunching data).</p>
  
  <p>Would it work, for example, to repeatedly read how much memory is available during, say, one minute, and see that if the number has been "oscillating" between different values all below, say, 4 MB, conclude that there's been some leak and that the application has become unusable?</p>