我的理解是 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/