11

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.

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

Who collects garbage from Metaspace?

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • I am not sure that I understood you but I thought that GC can collect garbage only from java heap but Metaspace is not inside java heap. – gstackoverflow Oct 20 '14 at 14:23

1 Answers1

11

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

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.

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.

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

Holger
  • 285,553
  • 42
  • 434
  • 765
  • so a different garbage collector algorithm would be used to collect meta-space ? (meaning its neither minor GC nor major GC) as the way of collecting meta space is different than collecting heap – amarnath harish Jul 16 '18 at 10:48
  • 1
    @amarnathharish well, there must be dedicated code for freeing the memory in the meta space, which is different to the other spaces, but since the artifacts stored in the meta space are associated with classes, it depends on the collection of classes to be able to reclaim this memory. And detecting that classes are unreachable usually happens in a major GC (aka “Full GC)”. – Holger Jul 16 '18 at 11:04
  • thanks for quick response, so full GC happens whenever old generation is filled up **or** Metaspace resize happens ? and when that happens it collects **both** old gen and Metaspace irrespective of what needs to be collected ? – amarnath harish Jul 16 '18 at 11:11
  • 3
    @amarnathharish you can see it that way or you consider that to reclaim memory in metaspace, it *needs* collecting the old generation (to identify unreachable classes) whereas when collecting the old generation due to filled up old generation, the knowledge about reclaimable metaspace artifacts comes as byproduct for free. – Holger Jul 16 '18 at 11:44