Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Class loading

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
For java classes in jar files/classpath, packages etc, does the JVM load all these classes onto the heap, irregardless of whether they are used or not?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure exactly what the JVM does. However, static blocks aren't excecuted until the class object is loaded (which is not necessary at load time). That indicates to me that the JVM doesn't fully recognize some classes until needed. That is not to say that the JVM doesn't use the classes in some way though.
Which is why, for example, we need to do something like Class.forName("blah.blah"), when we load and use JDBC drivers. Is there something at issue here, or is this a mere curiosity?
 
David Tan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nathaniel,
What happens is that I have a JAR file (containing some classes) included in the classpath. One of these classes actually references another (user-defined) class in another package. Let's name this class X residing in package xyz
When I attempt to execute a method of a class (e.g. A) in some package (abc), I encountered a "Class not found" exception. So I try to put that class into the JAR file and it works!
So it leads me to think that although X is not being called, the classloader will attempt to load it and find all the other classes that it references and load them as well. And that's why it encounter the "class not found" exception, since the classloader cannot find it in the JAR file in the classpath.
In addition, I believe this has to do with class-loading hierarchy, whereby there are a few classloaders for different classes at different locations (e.g. classpath, package etc)
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could imagine, that a class, which is in the jar-File, but not referenced by any other class, it will not be loaded.
But if the class is referenced, it would be a lot of mini disk-accesses, if every class was loaded when needed.
lib rt.jar is > 26 MB on my system, I don't think it is loaded completely every time.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
When the classloader loads a class, it needs to load all classes referenced by this class, even if the flow inside the class will not lead to a real creation of any object.
suppose:

the classloader must be able to find Y class, even if you always use 1 as a parameter here.
If Y is not in your classpath - X will not load.
However, loading of Y class itself is delayed. So - if Y references class Z which is not in your classpath - you will only find it later, when the JVM actually tries to instantiate Y.
Nimo.
 
I was her plaything! And so was this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic