java - jar 中的类什么时候进入 PermGen

标签 java tomcat classpath classloader permgen

我的理解是 PermGen(在某种意义上)将类代码保存在内存中。通常我们有很多 jar 文件引用了我们的类路径。当类路径中包含一个 jar 文件时(比如在 tomcat 的 lib 目录中),所有这些 jar 的类是否会自动加载到 PermGen 中?

在类似的问题中,一旦使用了一个 jar 文件的类,PermGen 是加载该 jar 文件中的所有类,还是仅加载使用的类(然后在必要时加载其余类文件) ?

最佳答案

这在某种程度上取决于类加载器和 JVM 的实现 - Java Virtual Machine specification是这样说的:

This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the Java programming language are respected, [...]

For example, an implementation may choose to resolve each symbolic reference in a class or interface individually, only when it is used (lazy or late resolution), or to resolve them all at once while the class is being verified (static resolution). This means that the resolution process may continue, in some implementations, after a class or interface has been initialized.

在实践中,理智的实现不应该仅仅因为文件中的一个类被加载就自动加载 JAR 文件中的所有内容,更不用说仅仅因为它在类路径上了。

关于java - jar 中的类什么时候进入 PermGen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690457/

相关文章:

java - 从流中过滤/删除无效的 xml 字符

tomcat - 如何在 Tomcat 上托管多个站点?

jsp - 使用 Spring Boot 的 struts2 - JSP 未呈现

java - Server Tomcat v8.5 Server at localhost 无法启动 Spring Project

java - 如何设置 Tomcat webapp 类路径

java - 关于 PECS java 泛型

java - Ibatis 支持 Sets 作为参数类吗?

java - (Java/Junit)我设置了类路径,现在无法在命令提示符下编译测试文件

java - 在jshell中查看类路径

java - 在java中使用GZIP压缩字节数组