java.lang.OutOfMemoryError: PermGen space error with Jetty

java.lang.OutOfMemoryError: PermGen space error with Jetty

我目前收到 java.lang.OutOfMemoryError: PermGen space。我正在使用 Jetty 和 Linux Ubuntu。我曾尝试阅读并尝试在之前的类似问题中提供的不同解决方案,但没有取得任何成功。一个类似的问题是

Dealing with “java.lang.OutOfMemoryError: PermGen space” error

但这些解决方案似乎使用 Tomcat 而不是 Jetty。如果我重新部署我的服务几次,我会不断收到内存不足错误。例如,为了对此进行测试,我将转到我的 webapps 文件夹,然后 运行ning touch *.xml 更新时间戳,然后重新 运行 jetty,然后出现内存不足错误。在我的 jetty 文件夹中(包括 bin、doc 等、lib、logs、modules,start.jar)我 运行ning

java -jar ../start.jar

但这给了我错误。然后我尝试了我在其他例子中读到的内容,例如:

java -jar ../start.jar JAVA_OPTS="-Xms256m -Xmx512m -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled"

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

-XX:MaxPermSize=128m

然后当我转到 firefox 并导航到 localhost:8080 并刷新时,在终端中我不断收到错误

java.lang.OutOfMemoryError: PermGen space

您可以在 Jetty 主文件夹的 start.ini 文件中添加这些参数。如果这不起作用,您可以尝试设置更高的 MaxPermSize,例如 1024m。

除了将 MaxPermSize 提高到更高的值(例如 1024m)之外,您无能为力:

-XX:MaxPermSize=1024m

这是一个常见问题,在 Jetty Documentation - Prevent Memory Leaks 部分也有解释:

Permgen problems

The JSP engine in Jetty is Jasper. This was originally developed under the Apache Tomcat project, but over time has been forked by many different projects. All jetty versions up to 6 used Apache-based Jasper exclusively, with Jetty 6 using Apache Jasper only for JSP2.0. With the advent of JSP 2.1, Jetty 6 switched to using Jasper from Sun's Glassfish project, which is now the reference implementation.

All forks of Jasper suffer from a problem whereby the permgen space can be put under pressure by using jsp tag files. This is because of the classloading architecture of the jsp implementation. Each jsp file is effectively compiled and its class loaded in its own classloader so as to allow for hot replacement. Each jsp that contains references to a tag file will compile the tag if necessary and then load it using its own classloader. If you have many jsps that refer to the same tag file, then the tag's class will be loaded over and over again into permgen space, once for each jsp. The relevant Glassfish bug report is bug # 3963, and the equivalent Apache Tomcat bug report is bug # 43878. The Apache Tomcat project has already closed this bug report with status WON'T FIX, however the Glassfish folks still have the bug report open and have scheduled it to be fixed. When the fix becomes available, the Jetty project will pick it up and incorporate into our release program.

Jetty 9.2+

在您的 ${jetty.base} 目录中,添加 jvm 模块 + 默认配置

[user]$ cd mybase
[mybase]$ java -jar /path/to/jetty-distribution/start.jar --add-to-start=jvm
INFO: jvm             initialised in ${jetty.base}/start.ini (appended)
[mybase]$ 

现在去编辑你的 ${jetty.base}/start.ini 并配置属性,取消注释那些你想要的东西(不要伪造取消注释 --exec)码头在启动时使用。

示例:

# --------------------------------------- 
# Module: jvm
--module=jvm
## JVM Configuration
## If JVM args are include in an ini file then --exec is needed
## to start a new JVM from start.jar with the extra args.
##
## If you wish to avoid an extra JVM running, place JVM args
## on the normal command line and do not use --exec
--exec
-Xmx1024m
-Xmn512m
-XX:MaxPermSize=128m
# -XX:+UseConcMarkSweepGC
# -XX:ParallelCMSThreads=2
# -XX:+CMSClassUnloadingEnabled
# -XX:+UseCMSCompactAtFullCollection
# -XX:CMSInitiatingOccupancyFraction=80
# -verbose:gc
# -XX:+PrintGCDateStamps
# -XX:+PrintGCTimeStamps
# -XX:+PrintGCDetails
# -XX:+PrintTenuringDistribution
# -XX:+PrintCommandLineFlags
# -XX:+DisableExplicitGC
# -Dorg.apache.jasper.compiler.disablejsr199=true

希望您能够简单地通过在 Jetty 本身中使用我的 ClassLoader Leak Prevention library. There are a lot of different mistakes that can cause these kinds of memory leaks, both in your own code and in third party libraries. More information about the problem, how to track it down and known offenders, can be found in this blog series of mine. Specifically note this bug 来解决这些问题,这可能会导致某些版本的此类泄漏。

这是一个较旧的问题,但这解决了我的问题:

contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");