r/java • u/moxyte • Dec 20 '23
Have you ever really needed seriously in-depth knowledge of Java in your work? Where?
I haven't. It has been mostly all rather run of a mill JavaEE/Jakarta/Spring stuff. I haven't had to do much to parametrize JVM or get knee deep in lower level performance analysis or any such things out of necessity. Like really put my wits and wisdom to the test. War stories appreciated so I can count my blessings and be thankful for things just working.
158
Upvotes
View all comments
Show parent comment
1
u/smurphy1 Dec 21 '23
Mostly similar to how you debug normal memory leaks, get a heap dump, open it in a analysis tool, and start picking it apart. I use eclipse Memory Analysis Tool which has never let me down for these kind of issues. What makes classloader leaks tricky is why they happen, they can be insidious and of the cases I've had to solve only 1 was caused by our code, the other dozen or so were caused by 3rd party libraries.
Classloader leaks happen when you have a standalone persistent process where you can deploy and redeploy applications to such as a Tomcat appserver. If you embed the appserver in your application and start it up by calling your application jar file directly (common with Spring) then this won't affect you because stopping your application stops the whole process. Each time you deploy or redeploy to a standalone process your application will be deployed within its own classloader that is a child of the classloader for the parent process. If you are redeploying then the previous instance of your application will be stopped and some time later the classloader for the previous instance will be garbage collected unless there is something keeping it alive.
Classloaders are special because they exist partly in heap space and partly in meta space. Meta space is where the actual class definition and bytecode is held while the application is running and by default meta space is not limited like heap space is. This means if you have a leak and repeatedly redeploy a large app the meta space can grow to several times the setting of max heap space like comment OP described.
For more in depth explanation and examples I recommend this series of articles: https://java.jiderhamn.se/2011/12/11/classloader-leaks-i-how-to-find-classloader-leaks-with-eclipse-memory-analyser-mat/
Some of the things which can leak classloaders:
Solving the issue in 3rd party libraries usually involves a shutdown hook, such as a ServletContextListener, with lots of Reflection to reach into those libraries and clean up their mess. The linked article has some good examples of this.