为什么当我将未使用的 JAR 包添加到类路径中时,JVM 会设置更大的堆大小

huangapple go评论121阅读模式
英文:

Why does the JVM sets a larger heap size when I add unused jars to the classpath

问题

我有一个在cpp程序内部使用JNI运行的JVM,它接收一个CLASSPATH参数。
我注意到,当我将JAR包添加到CLASSPATH中时,即使这些JAR包中的类没有被加载或以任何方式被调用,堆大小和使用量也会变大。

有人能解释一下为什么会发生这种情况吗?这是JVM尝试进行的某种内存优化吗?

我能改变这种行为吗?

英文:

I have a jvm that runs inside a cpp program using JNI and receives a CLASSPATH parameter.
I saw that when I add jars to the CLASSPATH the, heap size and usage is larger even when the classes in these jars are not loaded or called in any way.

Can someone explain why this happens? is it some memory optimization the JVM trying to do?

Can I change this behavior?

答案1

得分: 1

> 有人可以解释一下为什么会发生这种情况吗?

当一个类加载器初始化时,它会读取并缓存其类路径上每个JAR<sup>1</sup>文件的索引。我不确定这是急切地还是懒惰地完成的,但无论哪种情况,即使从给定的JAR中没有加载任何类,你最终都可能会缓存索引。 (类加载器通常不得不检查多个JAR才能找到它要查找的内容。)

(想想看,JVM 可能会将整个文件映射到内存中。我没有检查过JVM源代码,以了解它实际上的操作。)

> 这是JVM尝试进行的某种内存优化吗?

这是为了优化类和资源加载时间。缓存索引可以避免类加载器在每次加载类或其他资源时都需要重新读取它们。

> 我可以改变这个行为吗?

可能不能。

但问题在于,通过这样做可以节省的内存量相对较小。可能不值得担心。

而且,如果您真的担心这种情况所“浪费”的内存量,可以考虑重新组织应用程序类路径上的JAR文件,使它们只包含应用程序实际会使用的类和其他资源。 (有一些工具可以帮助您执行此类操作...)


<sup>1 - ... 或者ZIP文件。</sup>

英文:

> Can someone explain why this happens?

When a classloader initializes, it will read and cache the index for each JAR file<sup>1</sup> on its classpath. I'm not sure if this is done eagerly or lazily, but in either case you could end up with the index cached even though no classes were loaded from a given JAR . (The classloader often has to check multiple JARs to find what it is looking for.)

(Come to think of it, the JVM could be mapping the entire file into memory. I haven't checked the JVM source code to see what it actually does.)

> Is it some memory optimization the JVM trying to do?

It is optimizing class and resource load times. Caching the indexes avoids the classloader having to re-read them each time the classloader loads a class or other resource.

> Can I change this behavior?

Probably not.

But here's the thing. The amount of memory that you would save by doing this is relatively small. It is probably not worth worrying about.

And if you are really concerned by the amount of memory "wasted" by this, consider reorganizing the JAR files on your application's classpath so that they only contain classes and other resources that the application is actually going to use. (There are tools that can help you do this kind of thing ...)


<sup>1 - ... or ZIP file.</sup>

huangapple
  • 本文由 发表于 2020年10月1日 17:26:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64152511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定