最近,我在我的web应用程序中遇到了这个错误:
java.lang.OutOfMemoryError:永久生成空间
它是一个典型的Hibernate/JPA + IceFaces/JSF应用程序,运行在Tomcat 6和JDK 1.6上。 显然,这可能发生在重新部署应用程序几次之后。
是什么原因导致的,如何避免呢? 我该如何解决这个问题?
最近,我在我的web应用程序中遇到了这个错误:
java.lang.OutOfMemoryError:永久生成空间
它是一个典型的Hibernate/JPA + IceFaces/JSF应用程序,运行在Tomcat 6和JDK 1.6上。 显然,这可能发生在重新部署应用程序几次之后。
是什么原因导致的,如何避免呢? 我该如何解决这个问题?
当前回答
增加Tomcat内存
C:\Program Files\Apache软件基础\Tomcat 9.0\bin
或者任何使用tomcat的地方。并运行tomcat9w或任何您使用的版本。
然后跟着图片走
改变128到1024,也最大改变到1024或更多你想要的。
其他回答
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
First step in such case is to check whether the GC is allowed to unload classes from PermGen. The standard JVM is rather conservative in this regard – classes are born to live forever. So once loaded, classes stay in memory even if no code is using them anymore. This can become a problem when the application creates lots of classes dynamically and the generated classes are not needed for longer periods. In such a case, allowing the JVM to unload class definitions can be helpful. This can be achieved by adding just one configuration parameter to your startup scripts:
-XX:+CMSClassUnloadingEnabled
默认情况下,它被设置为false,因此要启用它,您需要显式地在Java选项中设置以下选项。如果你启用了CMSClassUnloadingEnabled, GC也会扫描PermGen并删除不再使用的类。请记住,此选项仅在UseConcMarkSweepGC也使用下面的选项启用时才有效。因此,当运行ParallelGC或串行GC时,请确保您已经通过指定将GC设置为CMS:
-XX:+UseConcMarkSweepGC
从Tomcat的bin目录打开tomcat7w,或者在开始菜单中键入Monitor Tomcat (打开一个带有各种服务信息的选项卡窗口)。 在Java Options文本区域中添加这一行: - xx: MaxPermSize = 128 “初始内存池”设置为“1024”(可选)。 “最大内存池”设置为“1024”(可选)。 单击Ok。 重启Tomcat服务。
我遇到了完全相同的问题,但不幸的是,建议的解决方案都不适合我。这个问题在部署过程中没有发生,我也没有进行任何热部署。
在我的例子中,这个问题每次都发生在我的web应用程序执行期间的同一时间点,同时(通过hibernate)连接到数据库。
这个链接(前面也提到过)提供了足够的内部组件来解决这个问题。将jdbc-(mysql)-driver从WEB-INF移到jre/lib/ext/文件夹中似乎已经解决了这个问题。这不是理想的解决方案,因为升级到更新的JRE需要重新安装驱动程序。 另一个可能导致类似问题的候选是log4j,因此您可能也想移动它
我有我们在这里谈论的问题,我的场景是eclipse-helios + tomcat + jsf,而您所做的是将一个简单的应用程序部署到tomcat。我在这里展示了同样的问题,解决方法如下。
在eclipse转到服务器选项卡双击注册服务器在我的情况下tomcat 7.0,它打开我的文件服务器一般注册信息。在“一般信息”部分单击“打开启动配置”链接,这将打开在虚拟机参数参数选项卡中添加的服务器选项的执行,最后添加这两个条目
-XX: MaxPermSize = 512m
-XX: PermSize = 512m
和准备好了。