Search code examples
javaclassloader

Count loaded classes


I'm running a webservice on apache tomcat 6.0.29 with sun jdk1.6.0_21, the webservice use spring, hibernate, axis 1.4, apache cxf, jaxb and some other libraries.

I monitor the tomcat instance with javamelody, and I can recognize a constant growth of loaded classes an the amount of loaded classes are still growing during online time.

So is there a way to find which classes are produced, shouldn't the garbage collector do an clean up.

These are my JAVA_OPTS

-Xmx768m -Xms512m -Xss4m  -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=128m -XX:PermSize=128m

Solution

  • Are you talking about loaded classes or loaded instances.

    Almost 100k classes is a lot. The entire RT of JRE 1.6 has about 17k classes including inner classes. 100k instances is not really all that much.

    Further, classes are not unloaded (excluding frameworks which are designed to do that, like OSGi modules). Instances however are garbage collected when necessary.

    If you do not have OutOfMemory exceptions (you have plenty of space) the garbage collector doesn't need to be all that aggressive. Startup tomcat with less memory and you'll see that the gc will start collecting more aggressively. I don't recommend doing that on production as you will decrease performance by putting pressure on the GC.

    Further, a tool like YourKit can tell you a lot about the objects in memory, including I believe how many are eligible for gc. Which doesn't mean that the gc must collect them, but that it may.