Java中的垃圾收集是否发生在java堆的perm区域?

时间:2014-05-14 11:44:53

标签: java garbage-collection jvm

我正在阅读this文章,并说

Permanent generation of Heap or Perm Area of Heap is somewhat special and it is used to store Meta data related to classes and method in JVM, it also hosts String pool provided by JVM as discussed in my string tutorial why String is immutable in Java. There are many opinions around whether garbage collection in Java happens in perm area of java heap or not, as per my knowledge this is something which is JVM dependent and happens at least in Sun's implementation of JVM. You can also try this by just creating millions of String and watching for Garbage collection or OutOfMemoryError

所以我有一个问题:

垃圾收集是否发生在堆的Perm区域。如果不是静态变量或方法以及字符串文字池如何收集垃圾?

2 个答案:

答案 0 :(得分:1)

垃圾收集也会在堆的permgen空间中发生。

当各个类加载器从内存中卸载时,静态变量被垃圾收集。

字符串文字池可能永远不会被垃圾收集。

修改 如果permgen已满,则会发生OutOfMemoryError : PermGen

答案 1 :(得分:1)

Grabage收集确实发生在permgen(基于Jvm实现)。我写了一个回答SO问题如何刻意填补烫发。 JVM加载permgen区域中的类等元数据。如果由于某种原因JVM在permgen上运行不足,它可以决定卸载在任何地方都没有引用的类。但是当GC运行或者根本运行时,从JVM到JVM的实现可能会有很大不同。

卸载类来创建空间是jvm在抛出“Outof memory:permgen space”之前的最后努力

请查看此问题SO Link这可能会有所帮助基本上这会显示如何通过阻止GC的permgen区域来填充permgen。