JVM and loading classes on classpath
Hi all,
I have a few basic questions about some class loading stuff, and I'm hoping to be pointed to a good tutorial or explanation page.
Basically, I'm wondering about the JVM loading classes from the classpath at runtime of an application. Let's say, for example, that I have a Java application, which has fifteen different third-party JAR files on the classpath. From each JAR file, I only use one class (let's assume for the sake of simplicity that the one class does not have any dependencies within the JAR file), and each JAR file contains 100 classes.
If I run my application, and if all fifteen JARs are on my classpath, will the JVM:
(1) Load ONLY the fifteen total classes I need?
(2) Load ALL the 1500 total classes from the JAR file?
(3) Do something else?
I'm wondering because I may have an issue coming up; it seems people may think it will take longer to load an application if there are numerous JARs or classes on the classpath.
An explanation would be great, and a tutorial or site would also be greatly beneficial.
I did a Google search for "java load class jvm" and other such stuff, and found that class loaders will always load the root Java classes, but I wasn't sure how to apply what I read to my problem.
Even if the JVM does load all 1500 classes, will this impact performance or startup time?
Thanks,
Dan
[1420 byte] By [
Djaunla] at [2007-11-27 8:26:16]

The JVM will only load classes as it needs them
> The JVM will only load classes as it needs them
So in other words, my choice (1) was correct?
I'd really like to find a document that explains this. I've found the JVM Specification (http://java.sun.com/docs/books/jvms/), but I can only find it talking about HOW classes are loaded, not WHEN classes are loaded. I'm probably looking in the wrong place. I'm looking at Loading, Linking, and Initialization: http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html
It explains that the JVM will load an initial class, which launches the JVM (it will initialize from the class with the main method), and then it gets in to how user-defined classloaders can load classes and other stuff.
What I'm not seeing is something saying that the JVM only loads classes as they are needed.
I'm not saying you're wrong, I just need to find something that will explain it so I can convince somebody that having a number of classes on the classpath will not affect performance.
Thanks
U could use a memory profiler like JProbe to find out which classes are loaded at what time.Rather than depending on any article, you can see for yourself when a class is loaded.
Actually, having a long list of jars probably will affect performance slightly, since they have to be searched for every class which is loaded (though each is indexed).
The clearest explanation of class loading strategies I've come across is a book, I'm affraid "Inside the Java 2 Virtual Machine" by Bill Venners.
Basically, when you load a class, any interfaces or superclasses are loaded, but no referenced classes. The class initially stores referenced classes as FQNs. The first time running code trys to follow a reference, the classloader that loaded the original class will be asked for the referenced one, and the FQN is replaced with a class reference.
The first time the class is "actively used" i.e. by a static field reference or new, then the class will undergo static initialisation.
Under no circumstances does the JVM sequentially scan a jar, the JVM can load, for example, from an online directory of classes where no scanning is possible.
So if I have an application in which I do not initialize some classes until I call them from the command line for example, those classes are considered referenced classes, and are stored as FQNs?
Therefore, if I have referenced classes in my application they will be stored as FQNs, but they will not be LOADED until they undergo static initialization?
Does the JVM have to search through the jar file to store referenced classes as FQNs, or does it only search when then class has to undergo static initialization?
Thanks for the helpful replies!
> So if I have an application in which I do not
> initialize some classes until I call them from the
> command line for example, those classes are
> considered referenced classes, and are stored as
> FQNs?
Yes.
>
> Therefore, if I have referenced classes in my
> application they will be stored as FQNs, but they
> will not be LOADED until they undergo static
> initialization?
Generally, though there are circumstances in which a class might be loaded but not initialized (e.g. Class clazz = ReferencedClass.class;
>
> Does the JVM have to search through the jar file to
> store referenced classes as FQNs, or does it only
> search when then class has to undergo static
> initialization?
>
No, the FQNs are placed in the class files by the compiler. Remember import statements are just a shorthand, class references are always compiled as FQNs.
This assumes you aren't doing dynamic loading with Class.forName() or ClassLoader.loadClass();
Alright malcolmmc, I think you've cleared up my questions. Well, one more. Does FQN = Fully Qualified Name?If I could assign duke stars (**** internal server errors) I'd dish them out.Thanks for the help,Dan
> > Well, one more. Does FQN = Fully Qualified Name?> Exactly