为Maven设置最大堆大小

3

我正在使用带有OSX Yosemite操作系统的MacBook Pro电脑。在我的Java项目中运行maven clean install时,我不断地遇到错误:

java.lang.OutOfMemoryError: Java heap space

我进行了一些谷歌搜索,并遵循建议添加环境变量以为maven设置更大的堆大小。

我编辑了~/.bash_profile,并添加了以下行:

setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m"

我再次运行了maven clean install,但是仍然遇到了OutOfMemoryError错误。如何设置maven使用最大1024M堆内存?请注意保留HTML标签。

=== 更新 ====

完整日志:

java.lang.OutOfMemoryError: Java heap space
    at com.sun.tools.javac.file.ZipFileIndex$ZipDirectory.findCENRecord(ZipFileIndex.java:552)
    at com.sun.tools.javac.file.ZipFileIndex$ZipDirectory.<init>(ZipFileIndex.java:497)
    at com.sun.tools.javac.file.ZipFileIndex.checkIndex(ZipFileIndex.java:191)
    at com.sun.tools.javac.file.ZipFileIndex.<init>(ZipFileIndex.java:137)
    at com.sun.tools.javac.file.ZipFileIndexCache.getZipFileIndex(ZipFileIndexCache.java:100)
    at com.sun.tools.javac.file.JavacFileManager.openArchive(JavacFileManager.java:559)
    at com.sun.tools.javac.file.JavacFileManager.openArchive(JavacFileManager.java:482)
    at com.sun.tools.javac.file.JavacFileManager.listContainer(JavacFileManager.java:368)
    at com.sun.tools.javac.file.JavacFileManager.list(JavacFileManager.java:644)
    at com.sun.tools.javac.jvm.ClassReader.fillIn(ClassReader.java:2446)
    at com.sun.tools.javac.jvm.ClassReader.complete(ClassReader.java:2143)
    at com.sun.tools.javac.code.Symbol.complete(Symbol.java:421)
    at com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:298)
    at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:459)
    at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)
    at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)
    at com.sun.tools.javac.comp.Enter.complete(Enter.java:484)
    at com.sun.tools.javac.comp.Enter.main(Enter.java:469)
    at com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:929)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
    at com.sun.tools.javac.main.Main.compile(Main.java:439)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:125)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:169)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:823)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
2个回答

2

虽然这是一个非常老的帖子,但我认为值得补充的是,在使用maven-compiler-plugin时可以使用以下配置:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.1</version>
     <configuration>
          <maxmem>512m</maxmem>
     </configuration>
</plugin>

0

可能问题出在由surefire插件运行的测试中。你应该在surefire的<configuration>标签中包含<argLine>选项,而不是MAVEN_OPTS:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Xmx1g -XX:MaxPermSize=256m</argLine>
    </configuration>
</plugin>

谢谢,但我不认为我正在使用surefire插件。 - user842225
你能分享更广泛的日志输出吗?你的应用程序中有任何测试吗? - Jakub Kubrynski
@ Jakub Kubrynski,完整的日志已添加,请查看我的更新。 - user842225
你能否使用 jmap -heap <MAVEN_PID> 命令检查一下你的堆设置? - Jakub Kubrynski
我无法通过您提供的命令获取Maven堆大小。但是,我查看了我的Java堆大小,InitialHeapSize = 6815736,MaxHeapSize = 35651584,PermSize = 21757952,MaxPermSize = 85983232。 - user842225

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接