Java 在两个 Web 应用程序之间共享实例时出现 Maven Jetty 插件 OutOfMemoryError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18669345/
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-12 10:01:46  来源:igfitidea点击:

Maven Jetty Plugin OutOfMemoryError When Sharing Instance Between Two Web Apps

javamavenjetty

提问by c12

I'm using the maven jetty plugin to run my two web applications. One web application is spring mvc UI and the other is a RESTful web application. I able to get the two web applications to communicate when I run two separate mvn jetty:run instances and assign different ports. I have successfully deploy both in the same jetty instance using the same port using the below maven pom.xml configuration. I eventually get a ava.lang.OutOfMemoryError: PermGen space error. What is the best workaround for this?

我正在使用 maven jetty 插件来运行我的两个 Web 应用程序。一个 Web 应用程序是 spring mvc UI,另一个是 RESTful Web 应用程序。当我运行两个单独的 mvn jetty:run 实例并分配不同的端口时,我能够让两个 Web 应用程序进行通信。我已经使用下面的 maven pom.xml 配置使用相同的端口成功地将两者部署在同一个码头实例中。我最终得到一个 ava.lang.OutOfMemoryError: PermGen space 错误。什么是最好的解决方法?

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.6.8.v20121106</version>
    <configuration>
        <jvmArgs>-Xmx2024m -Xms2024m</jvmArgs>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <webApp>
            <contextPath>/</contextPath>
        </webApp>
        <contextHandlers>           
            <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                <war>../../api/target/main-api.war</war>
                <contextPath>/test</contextPath>
            </contextHandler>
        </contextHandlers> 
    </configuration>
</plugin>

采纳答案by Snehal Patel

Add following jvm argument, if you get error regarding cannot allocate memory then try using lesser value (128 and 256)

添加以下 jvm 参数,如果出现关于无法分配内存的错误,请尝试使用较小的值(128 和 256)

-XX:PermSize=256M -XX:MaxPermSize=512M

Reference

参考

回答by sbridges

I eventually get a java.lang.OutOfMemoryError: PermGen space error

我最终得到一个 java.lang.OutOfMemoryError: PermGen space error

How long is eventually? Is one of the webapps redeploying frequently due to changes?

最后是多久?是否由于更改而频繁重新部署 Web 应用程序之一?

It is very easy to leak classes when redeploying a web app. I would run maven with this setting added to MAVEN_OPTS

重新部署 Web 应用程序时很容易泄漏类。我会将此设置添加到 MAVEN_OPTS 来运行 maven

-XX:+HeapDumpOnOutOfMemoryError

-XX:+HeapDumpOnOutOfMemoryError

Run until you get an out of memory error, then load the dump with eclipse matand see what is filling up your perm gen. Most likely your web app is leaking classes on redeploy.

运行直到出现内存不足错误,然后用eclipse mat加载转储,看看是什么填满了你的 perm gen。很可能您的 Web 应用程序在重新部署时泄漏类

回答by Digital Alchemist

It depends on which JVM instance require more memory. As example, if tests are forked (by default), and fails due OutOfMemoryError then try configure plugin which launching them:

这取决于哪个 JVM 实例需要更多内存。例如,如果测试是分叉的(默认情况下),并且由于 OutOfMemoryError 失败,那么尝试配置启动它们的插件:

   <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <argLine>-Xmx1024m</argLine>
        </configuration>
    </plugin>
   <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <argLine>-Xmx1024m</argLine>
        </configuration>
    </plugin>

OR

或者

Apart from heap memory. You have to increase perm size also to resolve that exception in maven use these variables in environment variable. And Sometimes is good also to extend perm memory size -

除了堆内存。您还必须增加 perm 大小以解决 maven 中的异常,在环境变量中使用这些变量。有时也可以扩展永久内存大小 -

Set the environment variable:

设置环境变量:

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

变量名:MAVEN_OPTS 变量值:-Xmx512m -XX:MaxPermSize=256m

Compiled data from resource

来自资源的编译数据

回答by Chris Harris

Try using Plumbr to diagnose any memory leak issues with both your web apps. http://plumbr.eu/

尝试使用 Plumbr 诊断您的两个 Web 应用程序的任何内存泄漏问题。 http://plumbr.eu/

回答by Andrew

Please clarify: what jvm version you are using, what operating system you are on, how many physical memory is installed on your computer. What happen if you reduce your memory requirements for example to 1400M (it could help if you run on 32bit jvm), i.e.:

请澄清:您使用的 jvm 版本,您使用的操作系统,您的计算机上安装了多少物理内存。如果您将内存要求降低到例如 1400M(如果您在 32 位 jvm 上运行可能会有所帮助)会发生什么,即:

<jvmArgs>-Xmx1400m -Xms1400m</jvmArgs>

回答by carlspring

Try running Jetty in forked mode like this:

尝试在分叉模式下运行 Jetty,如下所示:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.6.8.v20121106</version>
    <executions>
        <execution>
          <id>start-jetty</id>
          <!-- Set this to the appropriate phase:
               pre-integration-test, or earlier test-compile-->
          <phase>pre-integration-test</phase>
          <goals>
             <goal>run-forked</goal>
          </goals>
       </execution>
    </executions>

    <configuration>
        <jvmArgs>-Xmx2048m -Xms1536m -XX:PermSize=128m -XX:MaxPermSize=256m</jvmArgs>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <webApp>
            <contextPath>/</contextPath>
        </webApp>
        <contextHandlers>           
            <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                 <war>../../api/target/main-api.war</war>
                <contextPath>/test</contextPath>
            </contextHandler>
        </contextHandlers> 
    </configuration>
</plugin>

For more details check Running Jetty in a forked JVM.

有关更多详细信息,请查看在分叉的 JVM 中运行 Jetty

And... Make sure you really have 2048 MB of free RAM before starting this.

而且...在开始之前,请确保您确实有 2048 MB 的可用 RAM。