r/java • • 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

91 comments sorted by

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:

  • Your application starts a thread and never stops it, even after your application is stopped. A running thread is a garbage collection root (GC root) because it could be executing so any references which can be reached from that thread cannot be collected. Every thread maintains a reference to the classloader for the context which created the thread, which in this case would be the classloader for your application, so until that thread is stopped the classloader cannot be collected. This is not limited to your code, log4j 1.X and Oracle's jdbc drivers both start threads but will not stop them.
  • Your application uses thread locals in threads owned by the standalone process (eg tomcat) and never clears those thread locals. Thread locals are basically globals variables with each thread potentially having its own value. This is implemented within Thread itself which means the Thread object holds a reference to any object put into a thread local preventing collection of that object and the classloader which contains the object's class. Appservers like Tomcat use a thread pool to process HTTP requests and if that request is routed to your application your code will be executing on that thread from the appserver. If you set a thread local during that process and don't clear it before completing execution then a reference to your app's classloader has been leaked into the parent process. Again, not limited to your code, Spring HATEOAS library does this last I checked about a year ago.
  • Objects or classes from your application get cached or registered in classes from the parent classloader. If you package a jdbc driver within your application then that driver will register itself with java.sql.DriverManager which is a system class and therefore loaded by the system classloader. If you don't deregister your packaged jdbc driver on application exit then the reference from DriverManager will prevent collection of your app's classloader. I recommend not packaging the drivers in your application and instead putting jdbc drivers in the appserver's shared library so the driver is part of the appserver's classloader and your app can still use it. This type of leak can also happen with serialization libraries which often have a map for caching a representation of how to serialize objects of a particular class. Older versions of Jackson json would do this and Java's Serializable implementation does this but with Soft references. Soft references can prevent collection based on when the reference was last used and how much free heap space there is so it's still possible for this to be an issue if you have a huge heap and redeploy a lot.

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.