title: Java unit testing: how to measure memory footprint for method call
body: <p>Assuming I have a class that does some heavy processing, operating with several collections. What I want to do is to make sure that such operation can't lead to out-of-memory or even better I want to set a threshold of how much memory it can use.</p>
  
  <pre><code>class MyClass()
  {
     public void myMethod()
     {
        for(int i=0; i&lt;10000000; i++)
        {
           // Allocate some memory, may be several collections
        }
     }
  }
  
  class MyClassTest
  {
     @Test
     public void myMethod_makeSureMemoryFootprintIsNotBiggerThanMax()
     {
        new MyClass().myMethod(); 
        // How do I measure amount of memory it may try to allocate?
     }
  }
  </code></pre>
  
  <p>What is the right approach to do this? Or this is not possible/not feasible?</p>