java.lang.OutOfMemoryError:Maven 中的 Java 堆空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4066424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:11:09  来源:igfitidea点击:

java.lang.OutOfMemoryError: Java heap space in Maven

javamavenout-of-memory

提问by zjffdu

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. Anyone know other solutions for this problem ,BTW I am using maven 3.0

当我运行 maven 测试时,会发生 java.lang.OutOfMemoryError。我在谷歌上搜索解决方案并尝试过export MAVEN_OPTS=-Xmx1024m,但没有奏效。任何人都知道这个问题的其他解决方案,顺便说一句,我正在使用 maven 3.0

Thanks in advance

提前致谢

Paste the error message here when run "mvn test -e"

运行“mvn test -e”时在此处粘贴错误消息

Failed tests:
  warning(junit.framework.TestSuite)
  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

采纳答案by Pascal Thivent

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.

当我运行 maven 测试时,会发生 java.lang.OutOfMemoryError。我在谷歌上搜索解决方案并尝试导出 MAVEN_OPTS=-Xmx1024m,但没有奏效。

Setting the Xmxoptions using MAVEN_OPTSdoes work, it does configure the JVM used to start Maven. That being said, the maven-surefire-plugin forksa new JVM by default, and your MAVEN_OPTSare thus not passed.

设置Xmx选项 usingMAVEN_OPTS确实有效,它确实配置了用于启动 Maven 的 JVM。话虽如此,maven-surefire-plugin默认情况下会派生一个新的 JVM,MAVEN_OPTS因此您没有通过。

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

要配置 maven-surefire-plugin 使用的 JVM 的大小,您必须:

  • change the forkModeto never(which is be a not so good idea because Maven won't be isolated from the test) ~or~
  • use the argLineparameter (the right way):
  • 更改forkModenever(这不是一个好主意,因为 Maven 不会与测试隔离)~或~
  • 使用argLine参数(正确的方式):

In the later case, something like this:

在后一种情况下,是这样的:

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

ButI 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

参考

回答by Stephen C

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

问题很可能出在您要求 Maven 运行的单元测试之一中。

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.

因此,摆弄堆大小是错误的方法。相反,您应该查看导致 OOME 的单元测试,并试图找出是单元测试的错误还是它正在测试的代码的错误。

Start by looking at the stack trace. If there isn't one, run mvn ... testagain with the -eoption.

首先查看堆栈跟踪。如果没有,请mvn ... test使用该-e选项再次运行。

回答by user738048

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

对于 Maven 新手(像我一样),这里是 pom 的构建部分中的整个配置。干杯。

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <configuration>
            <argLine>-Xmx1024m</argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>

回答by Minh Dang

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

  1. 在 pom.xml 中添加这个配置

    <configuration><argLine>-Xmx1024m</argLine></configuration>
    
  2. 切换到使用的 JDK 1.7 而不是 1.6

回答by e2rabi

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

不仅是堆内存。还增加 perm 大小以解决 maven 中的异常在环境变量中使用这些变量。

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

Example :

例子 :

export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=500m"

回答by Max Hohenegger

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

为了暂时解决这个问题,我发现以下是最快的方法:

export JAVA_TOOL_OPTIONS="-Xmx1024m -Xms1024m"

回答by Narayan Yerrabachu

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

为了解决java.lang.OutOfMemoryError: Java heap space in Maven,尝试在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>