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?
MetaSpace grows automatically by default. Here, the garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size. It is removed from java 8. It is introduced in Java 8.
Metaspace is a native memory region that stores metadata for classes. As a class is loaded by the JVM, its metadata (i.e. its runtime representation in the JVM) is allocated into the Metaspace. The Metaspace occupancy grows as more and more classes are loaded.
Garbage collection is performed by a Java process, garbage collection, which removes data that is no longer needed from memory. For the best performance, use either the Garbage-First (G1) or Continuous Mark Sweep (CMS) collector. By default, DataStax Enterprise (DSE) uses the Garbage-First (G1) collector.
There is no guarantee that the actual GC will be triggered. System. gc() triggers a major GC. Hence, there is a risk of spending some time on the stop-the-world phase, depending on your garbage collector implementation.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With