最近,我在我的web应用程序中遇到了这个错误:

java.lang.OutOfMemoryError:永久生成空间

它是一个典型的Hibernate/JPA + IceFaces/JSF应用程序,运行在Tomcat 6和JDK 1.6上。 显然,这可能发生在重新部署应用程序几次之后。

是什么原因导致的,如何避免呢? 我该如何解决这个问题?


当前回答

目前最简单的答案是使用Java 8。

它不再专门为PermGen空间保留内存,允许PermGen内存与常规内存池混合。

请记住,您将必须删除所有非标准的-XXPermGen…JVM启动参数,如果你不想让Java 8抱怨它们什么都不做的话。

其他回答

如果存在真正的内存泄漏,增加永久生成大小或调整GC参数将不起作用。如果您的应用程序或某些第三方库使用,泄漏类加载器,唯一真正和永久的解决方案是找到这个泄漏并修复它。有许多工具可以帮助您,最近的一个工具是Plumbr,它刚刚发布了一个具有所需功能的新版本。

In case you are getting this in the eclipse IDE, even after setting the parameters --launcher.XXMaxPermSize, -XX:MaxPermSize, etc, still if you are getting the same error, it most likely is that the eclipse is using a buggy version of JRE which would have been installed by some third party applications and set to default. These buggy versions do not pick up the PermSize parameters and so no matter whatever you set, you still keep getting these memory errors. So, in your eclipse.ini add the following parameters:

-vm <path to the right JRE directory>/<name of javaw executable>

还要确保将eclipse中的首选项中的默认JRE设置为正确的java版本。

1)增加永久内存大小

The first thing one can do is to make the size of the permanent generation heap space bigger. This cannot be done with the usual –Xms(set initial heap size) and –Xmx(set maximum heap size) JVM arguments, since as mentioned, the permanent generation heap space is entirely separate from the regular Java Heap space, and these arguments set the space for this regular Java heap space. However, there are similar arguments which can be used(at least with the Sun/OpenJDK jvms) to make the size of the permanent generation heap bigger:

 -XX:MaxPermSize=128m

默认为64m。

2)启用扫地功能

另一种方法是允许类被卸载,这样你的PermGen就永远不会用完:

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

这种方法在过去对我很有效。不过,有一件事是,使用这些有一个显著的性能权衡,因为永久扫描将像额外的2个请求,你发出的每一个请求或类似的东西。您需要在使用和权衡之间取得平衡。

您可以找到这个错误的详细信息。

http://faisalbhagat.blogspot.com/2014/09/java-outofmemoryerror-permgen.html

目前最简单的答案是使用Java 8。

它不再专门为PermGen空间保留内存,允许PermGen内存与常规内存池混合。

请记住,您将必须删除所有非标准的-XXPermGen…JVM启动参数,如果你不想让Java 8抱怨它们什么都不做的话。

我也有类似的问题。 我的是基于JDK 7 + Maven 3.0.2 + Struts 2.0 +谷歌GUICE依赖注入的项目。

当我尝试运行mvn清洁包命令时,它显示以下错误和“BUILD FAILURE”发生

org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException;java.lang.reflect.InvocationTargetException: null java.lang.reflect.InvocationTargetException 由:java.lang.OutOfMemoryError: PermGen space引起

我尝试了上面所有有用的技巧,但不幸的是,没有一个对我有效。 对我有效的方法如下:=>

转到pom.xml 搜索<artifactId>maven-surefire-plugin</artifactId> 添加一个新的<configuration>元素和<argLine>子元素,其中pass -Xmx512m -XX:MaxPermSize=256m,如下所示=>

< configuration > < argLine > -Xmx512m -XX: MaxPermSize = 256m < / argLine > < / configuration >

希望能有所帮助,编程愉快:)