简体   繁体   English

GC是否从Metaspace收集垃圾?

[英]Does GC collects garbage from Metaspace?

Always I thought that garbage collector clear only heap and now I think so. 总是我认为垃圾收集器只清除堆,现在我想是的。

In java 8 permGen was deleted and it was replaced by Metaspace. 在java 8中,permGen被删除,它被Metaspace取代。

And As I understood Metaspace is garbage collected( https://stackoverflow.com/a/24075360/2674303 ) 正如我所知,Metaspace是垃圾收集( https://stackoverflow.com/a/24075360/2674303

Who collects garbage from Metaspace? 谁从Metaspace收集垃圾?

I think your confusion stems from the colloquial term “garbage collection” which is widely used but doesn't really describe what happens in a managed environment. 我认为你的困惑源于口语术语“垃圾收集”,它被广泛使用,但并没有真正描述在托管环境中发生的事情。

Memory Management is a complex process which is, simplified, about: 内存管理是一个复杂的过程,简化为:

  • Identifying the objects which are garbage, which is actually a process of determining which objects are reachable (read: not garbage) and consider everything not encountered to be garbage 识别垃圾对象,这实际上是确定哪些对象可以访问的过程 (读取:不是垃圾),并将未遇到的所有内容都视为垃圾
  • Enqueuing object references to reference queues and/or trigger finalization, if necessary 如有必要,将对象引用排入引用队列和/或触发终结
  • Reclaiming memory formerly occupied by garbage, which might also be the other way around: sometimes the alive objects are moved to a different memory space instead 回收以前由垃圾占用的内存,也可能是另一种方式:有时将活动对象移动到不同的内存空间

So for a memory space not consisting of Java objects, the first two points usually make not much sense which is what your question seems to be about. 因此,对于不包含Java对象的内存空间,前两点通常没有多大意义,这就是您的问题似乎是什么。 Algorithms addressing the first two points usually process the Java heap (defined as space containing ordinary Java object instances and similar structured data) only. 解决前两点的算法通常仅处理Java堆(定义为包含普通Java对象实例和类似结构化数据的空间)。

The statement you have linked, saying “Metaspace is GCed” seems to address mainly the third point. 您已链接的声明称“Metaspace is GCed”似乎主要针对第三点。 It's about the fact that memory within the Metaspace might get reclaimed if not needed anymore. 事实上,如果不再需要,Metaspace中的内存可能会被回收。 This does not imply that it requires a traversal of live references within the Metaspace or something similar. 这并不意味着它需要遍历Metaspace中的实时引用或类似的东西。 Obviously, class metadata are obsolete when their associated Class and ClassLoader have become unreachable, which are both ordinary (well, almost) objects living on the Java heap. 显然,当关联的ClassClassLoader变得无法访问时,类元数据已经过时,它们都是生活在Java堆上的普通(好的,几乎)对象。

So when the Metaspace size reaches a limit, a garbage collection will be triggered, but regarding the first two bullet above, it will not process the Metaspace as it is not the Metaspace which can tell you whether a Class has become unused. 因此,当Metaspace大小达到限制时,将触发垃圾收集,但是对于上面的前两个子弹,它将不会处理Metaspace,因为它不是Metaspace,它可以告诉您Class是否已经未使用。 It will be an ordinary garbage collection, but it will be a “Full GC” or whatever term the currently used GC algorithm has for the mode that includes collecting garbage within the memory segment (aka “generation”) which contains classes and class loaders. 它将是一个普通的垃圾收集,但它将是一个“Full GC”或当前使用的GC算法对于模式的任何术语,包括在包含类和类加载器的内存段(也称为“生成”)内收集垃圾。

Once Class and ClassLoader heap instances have been collected, their associated Metaspace data can be reclaimed as well during the cleanup. 收集ClassClassLoader堆实例后,还可以在清理期间回收其关联的Metaspace数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM