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

前端 未结 5 1633
栀梦
栀梦 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:43

    I found the solution to my problem - it may help others ;) When I was increasing the heap size I was increasing the heap size of eclipse application, not of my program (which I executed through eclipse) What I had to do is modify the execution commands before running my program.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-03 19:47

    Further to @Thijs Wouters response, to fix this issue in eclipse I did the following:

    • Added a new Run configuration under JUnit (Run>Run configuration>JUnit>New)
    • Within the arguments tab set VM arguments to "-Xms64m -Xmx256m" or higher if needs be
    0 讨论(0)
  • 2021-01-03 19:49

    I've just released a plugin for Eclipse that will automatically set the heap size on JUnit launchers for you. You can get it from http://code.google.com/p/junitlaunchfixer/ It works with Eclipse Europa, Ganymede and Galileo.

    0 讨论(0)
  • 2021-01-03 19:59

    Junit tests are run in a different vm as the Eclipse IDE. So it is that vm that is out of memory and not the Eclipse one.
    You can change the settings of the test vm in the run configurations of the test.
    You go to the run configurations and then under arguments, you can set the vm arguments.

    0 讨论(0)
提交回复
热议问题