title: Can a Java program detect that it's running low on heap space?
body: <p>I will be running a genetic algorithm on my roommate's computer for the whole weekend, and I'm afraid that it could run out of memory over such a long run. However, my algorithm works in such a way that would make it reasonably easy to trim less useful results, so if there was a way to tell when my program is about to run out of heap space, I could probably make room and keep going for some more time.</p>
  
  <p>Is there a way to be notified when the JVM is running out of heap space, before the OutOfMemoryError?</p>