nibblix said:
Consider the case where I have foo( ) having a peak heap usage of 150M.
I'd like to write a unit test case that ensures that future
refactoring/changes of foo() does not cause the heap to grow beyond
150M.
One way would be to set the Xmx to 150M but I'm looking for a way to do
this without causing the JVM to error out.
thanks,
n
There's no Unit Test reliable means of doing this.
The best you can do is use the Runtime memory methods to check that
current usage after running foo() is less than 150M. The problem with
this is that other factors may make the test pass or fail intermittently
- eg GC previous testcases mount up memory usage before GC can reclaimm etc.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
e.g.
public void testMemoryConsumptionIsBelowMaximumHeapSize() {
Runtime rt = Runtime.getRuntime();
assertTrue("Sanity test - starting memory is already beyond heap size
target", rt.totalMemory() < 150);
ClassUnderTest.foo();
assertTrue("Heap size target exceeded", rt.totalMemory() < 150);
}
It would also be a stress test of Foo() which is only being tested
within the single threaded nature of JUnit. It wouldn't be testing
Foo() in the nature of how it would be used in the application, where
other processing would be happening which might take memory consumption
over the limit.