JUnit Memory Usage In Eclipse
If you happen to run a lot of JUnit tests in one go with Eclipse, you may find that towards the end they start to slow down or throw OutOfMemoryExceptions. They do that because the Eclipse JUnit plugin creates a new instance of the TestCase for each test method, calls setup, test method, tearDown and then for some reason keeps the object around. This means that if your tearDown doesn't null out all the fields in the test class you can wind up with a whole lot of wasted memory.
You'll have to have a whole heap of atomic tests before you notice this, but with about 1200 integration tests, today we started getting out of memory exceptions. Fortunately, there were only a few base classes that we needed to add clean up code to and everything's running smoothly again.
It's pretty annoying that Eclipse is wasting memory like that – why should we waste developer time just because the JUnit plugin is clingy?

September 19th, 2006 at 12:52 pm
It’s not actually just the plugin…As far as I know this is what JUnit does all on it’s own..
If you’d like a better alternative that doesn’t suck you might try out TestNG instead. It and its eclipse plugin work very well. (It’ll also run your junit tests for you to ease the transition..)
September 19th, 2006 at 1:01 pm
We aren’t seeing these problems when the tests are run via ant which is why I suspected the Eclipse plugin. The tests may just be getting more memory when run via ant which would also avoid the problem so it’s not guaranteed to be the eclipse plugin.
October 3rd, 2006 at 10:26 pm
… we experienced the same JUnit behaviour in Eclipse. Our Testsuite contains now over 11000 Unit-Tests and we managed the problem by adding the -Xmx -Xms arguments to the JUnit running configuration.
Today the worst case scenario occured. The testsuite hung up at ~5000 Unit Tests without any exception. Spent the whole day setting new memory values and retry.
Which Java/JUnit/Eclipse version do you use ? We use Eclipse 3.0.1 with JUnit 3.8.1 and JDK 1.5.0.08
Maybe a newer JUnit version will work ?!?
October 4th, 2006 at 7:12 am
We’re using Eclipse 3.2 with JUnit 3.8.2 and JREs from 1.4.0 to 1.6 beta. JUnit 4 might be better but we still support Java 1.4 so don’t won’t to move our build process up to Java 1.5 and it’s too much effort to use two separate JREs for building vs running tests.
December 18th, 2006 at 4:19 pm
[...] suite的类。 作者: Cherami 原载: 遭遇OutOfMemoryError版权所有。转载时必须以链接形式注明作者和原始出处及本声明。 [...]
July 3rd, 2007 at 9:31 pm
I have the exact same problem. Mine occurs at somewhat lower numbers of test though due to the enormous size of things Hibernate and Spring generates for me in each test. Someone said something about using fork to start each test in a new jvm but I am wondering if not this is (only) an Ant feature?
December 7th, 2007 at 4:48 pm
Hi, I have also the same problem. I use Eclipse 3.2 with its builtin juint functionalities. Is there any good solution for this problem (in eclips) for the time being?