145

When I run maven test, java.lang.OutOfMemoryError happens. I googled it for solutions and have tried to export MAVEN_OPTS=-Xmx1024m, but it did not work. Anyone know other solutions for this problem? I am using maven 3.0

Paste the error message here when run mvn test -e

Failed tests:
  warning(junit.framework.TestSuite$1)
  testDefaultPigJob_1(com.snda.dw.pig.impl.DefaultPigJobLocalTest)
  testDefaultPigJob_2(com.snda.dw.pig.impl.DefaultPigJobLocalTest)

Tests run: 11, Failures: 3, Errors: 0, Skipped: 0

10/11/01 13:37:18 INFO executionengine.HExecutionEngine: Connecting to hadoop fi
le system at: file:///
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.063s
[INFO] Finished at: Mon Nov 01 13:37:18 PDT 2010
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
5:test (default-test) on project dw.pig: There are test failures.
[ERROR]
[ERROR] Please refer to E:\Code\Java\workspace\dw.pig\target\surefire-reports fo
r the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal o
rg.apache.maven.plugins:maven-surefire-plugin:2.5:test (default-test) on project
 dw.pig: There are test failures.

Please refer to E:\Code\Java\workspace\dw.pig\target\surefire-reports for the in
dividual test results.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:199)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:148)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:140)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje
ct(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje
ct(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBu
ild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(Lifecycl
eStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:314)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:151)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:445)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:168)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:132)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun
cher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav
a:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La
uncher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:
352)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures
.

Please refer to E:\Code\Java\workspace\dw.pig\target\surefire-reports for the in
dividual test results.
        at org.apache.maven.plugin.surefire.SurefirePlugin.execute(SurefirePlugi
n.java:629)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(Default
BuildPluginManager.java:107)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:195)
        ... 19 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
zjffdu
  • 25,496
  • 45
  • 109
  • 159
  • 1
    Maybe you have a memory leak in your test? Despite it being garbage collected, you can still create a memory leak in java: http://www.ibm.com/developerworks/library/j-leaks/ – anio Nov 01 '10 at 03:20

8 Answers8

179

When I run maven test, java.lang.OutOfMemoryError happens. I google it for solutions and have tried to export MAVEN_OPTS=-Xmx1024m, but it did not work.

Setting the Xmx options using MAVEN_OPTS does work, it does configure the JVM used to start Maven. That being said, the maven-surefire-plugin forks a new JVM by default, and your MAVEN_OPTS are thus not passed.

To configure the sizing of the JVM used by the maven-surefire-plugin, you would either have to:

  • change the forkMode to never (which is be a not so good idea because Maven won't be isolated from the test) ~or~
  • use the argLine parameter (the right way):

In the later case, something like this:

<configuration>
  <argLine>-Xmx1024m</argLine>
</configuration>

But I have to say that I tend to agree with Stephen here, there is very likely something wrong with one of your test and I'm not sure that giving more memory is the right solution to "solve" (hide?) your problem.

References

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Do you update the tag only within your parent pom.xml? – Kevin Meredith Sep 21 '12 at 16:16
  • 3
    `forkMode` has been deprecated: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#forkMode – Macarse May 02 '13 at 13:43
  • 1
    @Macarse `forkMode` has been deprecated, but I think only to be replaced with `forkCount` which has similar functionality. One way you could use MAVEN_OPTS is `${env.MAVEN_OPTS}` but that's apparently not recommended since it can vary from computer to computer (http://stackoverflow.com/a/10463133/32453). Also note that if you're using jacoco you're supposed to set argLine a different way http://stackoverflow.com/questions/12269558/maven-jacoco-plugin-error – rogerdpack Apr 30 '15 at 18:41
78

For those new to Maven (like me) here is the whole config that goes in the build section of your pom. Cheers.

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <argLine>-Xmx1024m</argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>
WizardsOfWor
  • 2,974
  • 29
  • 23
  • Your solution raises an `JUnitException: TestEngine with ID 'junit-jupiter' failed to discover test` when starting JUnit tests inside of IntelliJ. – Valerij Dobler Nov 15 '20 at 01:21
12

The chances are that the problem is in one of the unit tests that you've asked Maven to run.

As such, fiddling with the heap size is the wrong approach. Instead, you should be looking at the unit test that has caused the OOME, and trying to figure out if it is the fault of the unit test or the code that it is testing.

Start by looking at the stack trace. If there isn't one, run mvn ... test again with the -e option.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    @Stephen,I can pass the test case in eclipse after I set-Xmx1024m in run configuration, but it always throw OutOfMemoryError when I run "mvn test" in console, even when I add -e options "mvn test -DMAVEN_OPTS=-Xmx1024m" – zjffdu Nov 01 '10 at 04:32
  • 3
    @zjffdu - You completely missed my point! The purpose of adding "-e" is not to make the tests work. It is to find out why they don't work. – Stephen C Nov 01 '10 at 04:49
  • @Stephen, I add the 'e' option, but did not get enough useful information. – zjffdu Nov 01 '10 at 08:40
  • @zjffdu - it says *"Please refer to E:\Code\Java\workspace\dw.pig\target\surefire-reports for the individual test results"*. Did you?? – Stephen C Nov 01 '10 at 10:47
  • 1
    @zjffdu - in that case, you'll need to debug this the hard way, like you would other Java problems. – Stephen C Nov 01 '10 at 13:10
  • 2
    I get this problem while running GWT test cases, which simulate a full browser environment. Sometimes it's OK to increase the heap size. – djjeck Mar 04 '13 at 20:56
  • I was getting this error on mvn install, using "mvn install -DMAVEN_OPTS=-Xmx1024m" solved my problem! – Surekha Nov 07 '14 at 19:46
  • @Surekha - Your problem is different to the one the OP was getting. He was getting this while running tests. – Stephen C May 08 '19 at 15:41
  • 2
    @djjeck - Yes sometimes. But I don't think it is the right solution most of the time. – Stephen C May 08 '19 at 15:42
12

To temporarily work around this problem, I found the following to be the quickest way:

export JAVA_TOOL_OPTIONS="-Xmx1024m -Xms1024m"
Max Hohenegger
  • 1,609
  • 15
  • 19
8

I have solved this problem on my side by 2 ways:

  1. Adding this configuration in pom.xml

    <configuration><argLine>-Xmx1024m</argLine></configuration>
    
  2. Switch to used JDK 1.7 instead of 1.6

Minh Dang
  • 149
  • 1
  • 6
7

As mentioned in the top answers, as Surefire is creating a new JVM to run your tests, you want to pass that -Xmx1024m to the new JVM instead of the JVM where you start mvn test.

Just wanted to add that you can do that in your command like

mvn test -DargLine="-Xmx1024m"

It has equivalent effect with adding the JVM option to the Surefire plugin's configuration in pom.xml:

<configuration>
  <argLine>-Xmx1024m</argLine>
</configuration>
Instein
  • 2,484
  • 1
  • 9
  • 14
5

In order to resolve java.lang.OutOfMemoryError: Java heap space in Maven, try to configure below configuration in pom

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire-plugin.version}</version>
     <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <argLine>-XX:MaxPermSize=500M</argLine>
    </configuration>
</plugin>
Narayan Yerrabachu
  • 1,714
  • 1
  • 19
  • 31
  • 1
    +1 for mentioning MaxPermSize. If your out of memory error is caused by loading a bazillion classes like creating new HiveContext in your unit test, this is your solution. – swdev Feb 17 '18 at 01:51
  • 1
    Oracle completely removed Perm gen space in the JDK 8 release. So, setting -XX:MaxPermSize will give a warning: "OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0" – user674669 Jan 29 '21 at 11:35
-2

Not only heap memory. also increase perm size to resolve that exception in maven use these variables in environment variable.

variable name: MAVEN_OPTS
variable value: -Xmx512m -XX:MaxPermSize=256m

Example :

export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=500m"
e2rabi
  • 4,728
  • 9
  • 42
  • 69