Why does heap space run out only when running JUnit tests?

前端 未结 5 1632
栀梦
栀梦 2021-01-03 19:11

When running JUnit tests, I always seem to run into this error:

eclipse outOfMemoryError: heap space

I have monitored Eclipse wi

5条回答
  •  隐瞒了意图╮
    2021-01-03 19:45

    You probably have a memory leak in your JUnit tests. A common gotcha is this: Junit will create a new instance of a TestCase class for every test method in it And all instance variables will be kept around until JUnit terminates. That means: if you have a TestCase class with 50 test methods and an instance variable that is initialized with a 1MB object graph in your setUp() method, then that TestCase class will require 50MB heap space.

    Edit: the problem described above only exists in older versions of JUnit, I think it was fixed in JUnit 4.

提交回复
热议问题