JUnit java.lang.OutOfMemoryError when running all tests in a package

When loading all unit tests in a package, the make task raises the error java.lang.OutOfMemoryError: Java in heap space.

If I run all the tests in each subpackage, all the tests load and end just fine. Only when I try to run all the tests in the parent package does an OOM error occur.

I do not think that this problem should be solved by tuning the VM parameters. I tried to increase the maximum heap size and perm, and this did not solve the problem.

This makes me think that there is some problem of garbage collection between loading tests in different packages or that classes loading too intensively.

Is there a JUnit parameter that could solve these problems, or should the problem be solved by modifying or adding code to test cases?

+6
java unit-testing junit out-of-memory
source share
3 answers

You must set all the fields of the null test classes to tearDown() .

The reason is that JUnit creates an instance of one instance of the test class for each test. It saves this instance all the time in order to save the test results (success, failure, stack trace). Therefore, if you use fields, they will remain and you will run out of memory.

+9
source share

I had a similar problem using TestNG and tracked it down to the amount of log information that I generated on the console. As soon as I reduced this, I was able to run my test suite without memory problems.

+4
source share

GC works when the CPU has free time or low memory. If your tests fail, you might have a memory leak somewhere. (Yes, they exist in java too)

Look at circular links and static classes / variables. Tags are common IIRC causes for memory leaks. You should also look at jconsole.

+3
source share

All Articles