title: How do I know if I'm about to get an OutOfMemoryException?
body: <p>I have a program that processes high volumes of data, and can cache much of it for reuse with subsequent records in memory. The more I cache, the faster it works. But if I cache too much, boom, start over, and that takes a lot longer! </p>
  
  <p>I haven't been too successful trying to do anything after the exception occurs - I can't get enough memory to do anything. </p>
  
  <p>Also I've tried allocating a huge object, then de-allocating it right away, with inconsistent results. Maybe I'm doing something wrong? </p>
  
  <p>Anyway, what I'm stuck with is just setting a hardcoded limit on the # of cached objects that, from experience, seems to be low enough. Any better Ideas? thanks.</p>
  
  <h3>edit after answer</h3>
  
  <p>The following code seems to be doing exactly what I want:</p>
  
  <pre><code>Loop 
      Dim memFailPoint As MemoryFailPoint = Nothing
      Try
          memFailPoint = New MemoryFailPoint( mysize) ''// size of MB of several objects I'm about to add to cache 
          memFailPoint.Dispose()
      Catch ex As InsufficientMemoryException
          ''// dump the oldest items here
      End Try
     ''// do work
  next loop.
  </code></pre>
  
  <p>I need to test if it is slowing things down in this arrangement or not, but I can see the yellow line in Task Manager looking like a very healthy sawtooth pattern with a consistent top - yay!!</p>