Class loading
When a class is accessed for the first time (for example, when an object of the class is created), the class loader locates the class file and allocates its metadata in the Metaspace. The class loader owns this allocated Metaspace and the class loader instance itself is loaded onto the heap. Once loaded, subsequent references reuse the metadata of that same class.
There are two class loaders worth mentioning at this point: the bootstrap class loader (which is responsible for loading the class loaders themselves) and the application class loader. Both of these class loaders’ metadata reside permanently in Metaspace and consequently, are never garbage collected. Dynamic class loaders (and the classes they load) are, on the other hand, eligible for garbage collection.
This leads us to the release of memory from the Metaspace.