在增加内存(Maven)之后OutOfMemory异常

时间:2013-10-10 06:20:21

标签: java maven

当我尝试将Apache Stanbol安装为本地实例时,我收到以下异常:

ERROR] Java heap space -> [Help 1]
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2786)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
    at aQute.lib.osgi.EmbeddedResource.copy(EmbeddedResource.java:62)
    at aQute.lib.osgi.EmbeddedResource.collect(EmbeddedResource.java:51)
    at aQute.lib.osgi.EmbeddedResource.build(EmbeddedResource.java:33)
    at aQute.lib.osgi.EmbeddedResource.build(EmbeddedResource.java:71)
    at aQute.lib.osgi.Verifier.getBundleClassPath(Verifier.java:255)
    at aQute.lib.osgi.Verifier.<init>(Verifier.java:204)
    at aQute.lib.osgi.Builder.doVerify(Builder.java:434)
    at aQute.lib.osgi.Builder.build(Builder.java:104)
    at org.apache.felix.bundleplugin.BundlePlugin.buildOSGiBundle(BundlePlugin.java:547)
    at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:347)
    at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:264)
    at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:255)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    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)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[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 read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/OutOfMemoryError

我使用以下命令增加了JVM内存,但我仍然遇到同样的错误。我的RAM是4 GB。

  

导出MAVEN_OPTS =“ - Xmx3000m -XX:MaxPermSize = 512m”

如何解决并解决此问题?

谢谢,

3 个答案:

答案 0 :(得分:0)

要设置的环境变量是MAVEN_OPTS,例如MAVEN_OPTS = -Xmx1024m。仅当您将编译器插件设置为fork javac到新JVM时,pom中的maxmem配置才适用。否则,插件在与Maven相同的VM内部运行,因此在通过MAVEN_OPTS在命令行上传递的内存中运行。

在Windows 7下设置MAVEN_OPTS:

  1. 右键单击“我的电脑”,然后选择“属性”
  2. 单击“系统属性”左侧导航中的“高级系统设置”链接以显示“高级系统属性”
  3. 转到“高级”选项卡,然后单击“高级系统属性”配置窗口底部的“环境变量”按钮
  4. 创建新用户变量,将变量名称设置为MAVEN_OPTS并将变量值设置为-Xmx1024m(或更多)
  5. 打开一个新的命令窗口并运行mvn。

答案 1 :(得分:0)

如果在构建过程中有大量资源过滤且没有足够的堆空间分配给Maven,则会在OSGI环境中发生这种情况。

如果你指定了这样的资源和过滤器,它应该解决问题......

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>**/*.xml</exclude>
        </excludes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/filtered-resources</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>

<filters>
    <filter>src/main/etc/org.yourcompany.yourproject.cfg</filter>
</filters>
  

注意:指定了过滤器文件(以.cfg结尾的文件)         如果没有这个,您将在尝试解析资源令牌时收到OutOfMemory错误。因此嵌入式资源阵列复制。

答案 2 :(得分:-1)

增加内存不是OOM错误的解决方案。它可以是暂时的缓解,但不是永久的解决方案。您需要修复导致内存泄漏的代码。

从异常看来,似乎有一些代码复制数组,并且它以递归方式执行,导致堆内存溢出。

How to find a Java Memory Leak