2

I'm using JDK 1.6 to run a small application. However I set a very massive classpath which includes a lot of classes.

When I run the application will all classes in the classloader been loaded even if they're not actually used in my application? If not, how to force the classloader do so, and if yes, how to avoid it? Thanks!

E.g.,I'm using ant 1.7 to run my application.

Best Regards, Robert Ji

Robert Ji
  • 35
  • 3

3 Answers3

6

No, The ClassLoader loads the class when the class is needed in memory. It doesn't load all classes at once as it can run out of memory.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2

They are loaded when needed. But what "when needed" means, might depend on the classloader. Typically, when a class is loaded it also checks the existence of all classes it references - and it might also load them.

You can check it by adding the option -verbose to your Java JVM, it outputs then all the classes it loads, and from where.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
1

To my knowledge it's impossible to load all classes if they are not accessed explicitly. Class in only loaded when it's constructor or any other static member is first accessed, this rule applies to nested classes as well.

ioseb
  • 16,625
  • 3
  • 33
  • 29
  • 1
    it is not impossible to load all classes in classpath.. I have upvoted, but you should correct that sentence. If you want , you can load all classes in your classpath, nothing can stop you. – Gursel Koca Jun 07 '11 at 13:51