如何修复 PermGen 内存问题

How to fixed PermGen memory issue

登录系统时浏览器出现如下错误

java.lang.RuntimeException: javax.servlet.ServletException: java.lang.OutOfMemoryError: PermGen space com.opensymphony.sitemesh.webapp.decorator.BaseWebAppDecorator.render(BaseWebAppDecorator.java:39) com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:84)

优化您的程序或使用 运行 时间参数增加 java 堆大小。(-Xxx)

java.lang.OutOfMemoryError

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.

增加 JVM 的堆大小

可以使用命令行选项增加 JVM 分配的堆大小这里我们有 3 个选项

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

PermGen中OutofMemory的常见原因是ClassLoader。每当将 class 加载到 JVM 中时,其所有元数据以及类加载器都会保存在 PermGen 区域中,当加载它们的类加载器准备好进行垃圾回收时,它们将被垃圾回收。如果 Classloader 发生内存泄漏,那么它加载的所有 classes 将保留在内存中,并在您重复几次后导致 permGen 内存不足。

现在有两种方法可以解决这个问题: 1. 查找Memory Leak的原因或者是否有内存泄漏。 2. 使用 JVM 参数 -XX:MaxPermSize 和 -XX:PermSize 增加 PermGen Space 的大小。

java.lang.OutOfMemoryError:PermGen space

两步解决方案。

第一步:更改您的应用程序启动配置并添加(如果存在则增加)-XX:MaxPermSize 参数。这是一个临时步骤,就好像这是一个实时系统一样,额外的内存很可能也会在某个时候在 future.But 中被填满,这将暂时修复错误。

第二步:释放不需要的资源。确保关闭所有数据库连接。加载和卸载 类 judiciously.restructure 您编程一次处理少量数据。分析并修复内存泄漏

重要的是要知道,PermGenError 是在每次 depolyment 之后发生,还是在几次 depolyment 之后发生。

如果是第一种情况,只需增加PermGen 的内存大小即可。

如果是第二种,那么你的代码中有些死牛。在这种情况下,增加内存大小只会推迟错误,因为每次部署后您的内存大小仍在增加。你必须分析你的应用程序,例如使用 jvisualvm (here is nice tutorial) and find some dead cows. It is important to understand, that PermGenError is not about the classes itself, but about classLoaders (Classloader leaks: the dreaded "java.lang.OutOfMemoryError: PermGen space" exception)。

就我而言(Glssfish 服务器),问题是添加到 Web 应用程序的 Log4j2 库。我还必须将它们添加到服务器库 (domain-dir/lib),以便根据 Glassfish Class Loaders Hierarchy 它们由 Common classLoader 加载。