javagarbage-collectionjvmmetaspace

Can class loaders or classes be dead in JVM?


Can class loaders or class be dead in JVM? I was reading a blog Oracle - Garbage collection and metaspace. The author wrote garbage collection is induced on metaspace when the max size is reached as a result dead classloaders and classes are garbage collected

I wanted to understand like objects become unreachable can the same be applied for classloaders and classes as well? If yes then how.


Solution

  • I wanted to understand like objects become unreachable can the same be applied for classloaders and classes as well?

    Yes, classloaders and classes can become unreachable under certain circumstances.

    In short, if there is any way for an application to find or use a (loaded) class or classloader, it is reachable. Otherwise it us unreachable.

    If yes then how.

    By getting rid of all paths that make the classloader and its classes reachable. Like you would to make an ordinary object.

    (Except that it is harder to achieve because many of the reachability paths are behind the scenes and application code cannot break them directly. Hence the issue that applications that make extensive use of dynamic class loading, dynamic proxies and the like may suffer from metaspace leaks.)