简体   繁体   English

系统类加载器是否会加载类路径中的所有类,即使它们实际上并未使用?

[英]Does system classloader load all classes in classpath even if they're not actually used?

I'm using JDK 1.6 to run a small application.我正在使用 JDK 1.6 来运行一个小型应用程序。 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!谢谢!

Eg,I'm using ant 1.7 to run my application.例如,我正在使用 ant 1.7 来运行我的应用程序。

Best Regards, Robert Ji最好的问候,罗伯特·吉

No, The ClassLoader loads the class when the class is needed in memory.不,当 memory 中需要 class 时,ClassLoader 会加载 class。 It doesn't load all classes at once as it can run out of memory.它不会一次加载所有类,因为它可能会用完 memory。

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.通常,当加载 class 时,它还会检查它引用的所有类的存在 - 它也可能会加载它们。

You can check it by adding the option -verbose to your Java JVM, it outputs then all the classes it loads, and from where.您可以通过将选项-verbose添加到 Java JVM 来检查它,然后输出它加载的所有类以及从何处加载。

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. Class 仅在其构造函数或任何其他 static 成员首次访问时加载,此规则也适用于嵌套类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM