1

I have an application that runs as both a desktop application and a server application. When I run as a server and take a heap dump of my application, I still see a few desktop-related classes getting loaded (but all have zero instances):

java.awt.image.RenderedImage 
java.awt.Image source
java.awt.image.WritableRenderedImage 
java.awt.Transparency 
java.awt.image.BufferedImage 
java.awt.image.ImageObserver 
java.awt.MenuContainer 
java.awt.Component 
java.awt.Graphics 
java.awt.Graphics2D 

I think in server mode I don't reference any GUI related classes - i.e. my main class makes the server/desktop determination and then uses reflection to either instantiate a server class or GUI-containing class. My GUI is Swing-based, and no swing-related classes show up in the heap dump, so I'm "nearly" certain that the above references aren't from any of my code. That leaves the third-party libraries as the possible culprit (or I simply overlooked something).

Either way, how do I determine who is causing these classes to load?

wolfman
  • 123
  • 9
  • One thing you can do, is to check if the class is loaded, "https://stackoverflow.com/questions/482633/in-java-is-it-possible-to-know-whether-a-class-has-already-been-loaded" Since you know it is, you can start paring down your code until you find which class is causing the awt classes to be loaded. Note that anytime you want to work with images, you need awt classes. – matt Nov 11 '22 at 11:00
  • You could also extend classloader so that it prints classes as they're loaded. Then you'll see when the awt classes get loaded. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#getSystemClassLoader() – matt Nov 11 '22 at 11:06
  • @matt - not sure how extending the system class loader would help - aren't the classes I mentioned loaded by the Platform class loader? As per the docs, the system class loader is for loading application classes along the classpath. The above are in rt.jar which isn't part of the app classpath. – wolfman Nov 11 '22 at 20:59
  • I'm not saying you need to replace the system class loader, but the class loader loading your classes.. You'll need a minimal reproducible example to get better suggestions I suspect. There are a couple ways to deal with the awt classes. You could use modules and not include the desktop module in a jlink artifact? You could run your server in headless mode? – matt Nov 13 '22 at 08:43

0 Answers0