Loading classes with many methods is very expensive

Martin Buchholz martinrb at google.com
Wed Oct 22 22:53:13 UTC 2014

Here at Google we have both kinds of scalability problems - loading classes
from a classpath with 10,000 jars, and loading a single class file packed
with the maximal number of methods.  This message is about the latter.

If you have a class with ~64k methods with a superclass that also has ~64k
methods, class loading that single class will cost you ~30sec and calling
Class.getMethods another ~10sec.  Both are unacceptably slow. I think both
are due to O(N^2) algorithms, the first in hotspot, and the second in

I have the start of a fix for Class.java, but it makes the common case
slower.  A really good fix is harder to find.  In general, I think
Class.java could benefit from some performance-oriented rework.  Is anyone
else working on class loading performance, especially in hotspot?

Here's the benchmark (that could perhaps be integrated into openjdk even
without a fix)


Base class load time: 186.44 ms
getDeclaredMethods: Methods: 65521, Total time: 43.27 ms, Time per method:
0.0007 ms
getMethods        : Methods: 65530, Total time: 60.82 ms, Time per method:
0.0009 ms
Derived class load time: 33440.13 ms
getDeclaredMethods: Methods: 65521, Total time: 39.71 ms, Time per method:
0.0006 ms
getMethods        : Methods: 65530, Total time: 11582.54 ms, Time per
method: 0.1768 ms

More information about the core-libs-dev mailing list