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

47,276

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
47,276
smauel
Author by

smauel

currently at uni studying for a computer science degree. Recently finished a placement year, and have a number of personal projects underway for learning experience

Updated on April 16, 2020

Comments

  • smauel
    smauel about 4 years

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

    eclipse outOfMemoryError: heap space

    I have monitored Eclipse with JConsole and heap memory peaks at about 150MB. I have set heap memory to 1GB.

    I am using the following arguments when starting Eclipse:

    -vm "C:\Program Files\Java\jre1.5.0_08\bin\javaw.exe" -vmargs -Xmx1024M
    -XX:MaxPermSize=128M -Dcom.sun.management.jmxremote.port=8999
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    

    Does anyone know what may be causing this issue? It happens only when running JUnit tests.

  • dantiston
    dantiston about 9 years
    So what's the solution to this? To set up your 1MB object in a static @BeforeClass method?
  • Michael Borgwardt
    Michael Borgwardt about 9 years
    @dantiston: Nope, to set the reference to null in a teardown method. But I think current versions of JUnit work differently and don't have this problem anyway.
  • Torben
    Torben over 6 years
    I don't know if the situation was improved between 3 and 4, but at least with 4.11 we need to nullify references in test class instances in @After-methods to prevent the test from running into OOME.