Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is classloader leak?

I see the methods in google how to resolve Classloader leaks but nowhere it is explained what is classloader leak ?

Say i load a object Person person = new Person() then Person class will be loaded by class loader.Person class object it will be stay in metaspace(Prior to java 8 it will stay in permgen) for forever even it Person object(not person class object) is not used any where in application . Will it be considered as memory leak ?

like image 250
scott miles Avatar asked Oct 17 '16 06:10

scott miles


Video Answer


2 Answers

Class loader leaks may happen when reloading or redeploying an application within an application server but not when restarting the whole server. Each time you restart an application in an application Server e.g. Tomcat, then a new class loader will be created in order to load the classes of the application. At the same time the old class loader is not used anymore. Since all references to the old classloader has been removed, it should be available now for garbage collection. In case that not all references to the old class loader has been removed, it cannot be garbage collected and you have a classloader leak.

A quite common cause of class loader leaks is the use of shared libraries between multiple applications within one application server. If e.g. such a library holds a static reference to a web applications class loader then this classloader cannot be garbage collected, even after the application has stopped.

like image 106
eztam Avatar answered Oct 17 '22 11:10

eztam


For somebody to encounter this later. A custom java classloader can be used within any java application to reload new versions of a class into already running jvm. There is lots of scenarios where this is needed. So you start your app(ie. jvm) then edit code of a class it loaded, then reload the new version to app using custom classloader without shutting down your app(jvm). There is strict rules when class reloading works like a charm. Break the rules and you have the class loader memory leak. You can monitor situation of loaded classes and instance counts and deltas and heap space by loading Java VisualJVM application from JDK bin directory. Easiest way is to load VisualJVM from within app itself with PID as parameter.

like image 40
Kurskinen Avatar answered Oct 17 '22 11:10

Kurskinen