GC是否从Metaspace收集垃圾? [英] Does GC collects garbage from Metaspace?

查看:412
本文介绍了GC是否从Metaspace收集垃圾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直以为垃圾收集器只能清除堆,而现在我是这样认为的.

Always I thought that garbage collector clear only heap and now I think so.

在Java 8中,permGen被删除,并由Metaspace代替.

In java 8 permGen was deleted and it was replaced by Metaspace.

据我所知,元空间是垃圾收集的( https://stackoverflow.com/a/24075360/2674303 )

And As I understood Metaspace is garbage collected(https://stackoverflow.com/a/24075360/2674303)

谁从Metaspace收集垃圾?

Who collects garbage from 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

因此,对于不是由Java对象组成的内存空间,前两点通常没有多大意义,这正是您的问题所在.解决前两点的算法通常只处理Java堆(定义为包含普通Java对象实例和类似结构化数据的空间).

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. Algorithms addressing the first two points usually process the Java heap (defined as space containing ordinary Java object instances and similar structured data) only.

您链接的语句说元空间已被GC处理"似乎主要解决了第三点.关于以下事实:如果不再需要元空间中的内存,则可能会对其进行回收.这并不意味着它需要遍历元空间或类似空间中的实时引用.显然,当类元数据的相关联的ClassClassLoader变得不可达时,它们都是过时的,它们都是生活在Java堆上的普通对象(差不多).

The statement you have linked, saying "Metaspace is GCed" seems to address mainly the third point. It’s about the fact that memory within the Metaspace might get reclaimed if not needed anymore. This does not imply that it requires a traversal of live references within the Metaspace or something similar. 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.

因此,当Metaspace大小达到限制时,将触发垃圾回收,但是对于上面的前两个项目符号,它将不会处理Metaspace,因为不是Metaspace可以告诉您Class是否已变为没用过.这将是普通的垃圾回收,但将是完整GC"或当前使用的GC算法所具有的术语,该模式包括在包含类和类加载器的内存段(也称为生成")中收集垃圾.

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. 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.

一旦收集了ClassClassLoader堆实例,它们的关联元空间数据也可以在清理期间回收.

Once Class and ClassLoader heap instances have been collected, their associated Metaspace data can be reclaimed as well during the cleanup.

这篇关于GC是否从Metaspace收集垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆