Java 8 和 11 ParallelGC 是否遵循这些 JVM 设置 MinHeapFreeRatio 和 MaxHeapFreeRatio?

发布于 2025-01-11 13:50:30 字数 472 浏览 0 评论 0原文

我们有一个 Java 应用程序,其中应用了 MinHeapFreeRatio=20 和 MaxHeapFreeRatio=40,并使用 ParallelGC。然而,有些文章说,这些 JVM 设置(最小和最大空闲堆比率)不适用于 ParallelGC。然而,使用 JDK8 和 ParallelGC,我们注意到空闲堆比率在我们设置的限制内。然而,在 JDK 11 和 ParallelGC 中,我们注意到空闲堆比率并未得到满足。 鼓励 JVM 进行 GC 而不是增加堆?< /a>

对于无论 JDK 版本如何的 ParallelGC 是否遵循这些设置,我们都存在一些困惑?这些设置是否适用于 G1GC 和 CMS GC? 您能否分享您的专家对此问题的看法?

We have a Java application where we have applied the MinHeapFreeRatio=20 and MaxHeapFreeRatio=40, and using ParallelGC. However some of articles says that, these JVM settings (Min and Max free heap ratio) is not applicable for ParallelGC. However, with JDK8 and with ParallelGC we noticed that free heap ratio were within the limit we had set. However with JDK 11 and with ParallelGC, we are noticing that free heap ratio is not getting honored.
Encourage the JVM to GC rather than grow the heap?

We have some confusions around whether ParallelGC with irrespective of JDK version honors these settings or not? are these settings applicable for G1GC and CMS GC?
Can you please share your experts views around this query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2025-01-18 13:50:30

关于不同JDK版本的ParallelGC

我确信Jdk11也支持ParallelGC,并且还支持Min/MaxHeapFreeRatio参数。

  1. 以下代码取自OpenJdk11源码
    代码。 (源代码链接)
size_t PSAdaptiveSizePolicy::calculated_old_free_size_in_bytes() const {
    size_t free_size = (size_t)(_promo_size + avg_promoted()->padded_average());
    size_t live = ParallelScavengeHeap::heap()->old_gen()->used_in_bytes();
    if (MinHeapFreeRatio != 0) {
        size_t min_free = calculate_free_based_on_live(live, MinHeapFreeRatio);
        free_size = MAX2(free_size, min_free);
    }
    if (MaxHeapFreeRatio != 100) {
        size_t max_free = calculate_free_based_on_live(live, MaxHeapFreeRatio);
        free_size = MIN2(max_free, free_size);
    }
    return free_size;
}
  1. 我在OpenJdk8的源码中找到了完全相同的方法。(源代码链接< /a>)

因此,我可以确认Min/MaxHeapFreeRatio参数可以正常使用通过不同JDK版本的ParallelGC。

关于参数设置在JDK11中不生效

这个参数在某些情况下确实会失效,但在解释之前,我想先解释一下Min/MaxHeapFreeRatio到底做了什么。该参数直接作用于Old Gen,用于控制老年代中剩余空间在整个老年代中的比例,而不是整个堆中剩余空间在整个堆中的比例!

我尝试用图来详细解释。下图是ParallelGC堆内存的结构图:

在此处输入图像描述

Use Size:指GC后old gen中存活对象所占用的空间。 Free Size:是old gen中当前剩余未使用的空间

通过下面的公式,我们可以计算出old gen中剩余空间占old gen总空间的比例。

HeapFreeRatio = (Free Size) / (Old Gen Size) * 100

ParallelGC 通过在 GC 后动态调整 Old Gen 的大小,使 HeapFreeRatio 满足 Min/MaxHeapFreeRatio 要求。

Old Gen Size = Free Size + Use Size

特殊情况

当Use Size太大时,Min/MaxHeapFreeRatio确实会失败。

随着Use Size变大,ParallelGC必须通过扩大Old Gen来继续满足Min/MaxHeapFreeRatio的要求。又因为当前堆大小 = Young Gen + Old Gen
,所以扩大Old Gen的大小会使得Current Heap Size扩大,但是Heap Size是有最大上限的。于是就会出现下图(Current == Max):

enter image description here

此时,ParallelGC 已经无法通过扩展 Old Gen 来满足 Min/MaxHeapFreeRatio 的要求,但如果 Use Size 继续增长时,会出现以下情况:

在此处输入图像描述

在上述情况下,Use Size 太大而 Free Size 变小,因此 (Free Size / Old Gen) * 100 <最小堆空闲比率。此时,就会出现与你的预期不符的现象。

总之,

当当前堆大小 时,Min/MaxHeapFreeRatio 是一个软限制。 Max Heap Size,ParallelGC可以保证HeapFreeRatio
满足要求。但当Current Heap Size == Max Heap Size时,ParallelGC无法保证HeapFreeRatio满足要求。如果您对此仍有疑问,希望与您进一步沟通。

About ParallelGC for different JDK versions

I am sure that Jdk11 also supports ParallelGC, and also supports Min/MaxHeapFreeRatio parameters.

  1. The following code is taken from OpenJdk11 source
    code. (source code link)
size_t PSAdaptiveSizePolicy::calculated_old_free_size_in_bytes() const {
    size_t free_size = (size_t)(_promo_size + avg_promoted()->padded_average());
    size_t live = ParallelScavengeHeap::heap()->old_gen()->used_in_bytes();
    if (MinHeapFreeRatio != 0) {
        size_t min_free = calculate_free_based_on_live(live, MinHeapFreeRatio);
        free_size = MAX2(free_size, min_free);
    }
    if (MaxHeapFreeRatio != 100) {
        size_t max_free = calculate_free_based_on_live(live, MaxHeapFreeRatio);
        free_size = MIN2(max_free, free_size);
    }
    return free_size;
}
  1. I found the exact same method in the source code of OpenJdk8。(source code link)

Therefore, I can confirm that the Min/MaxHeapFreeRatio parameters can be used normally by ParallelGC of different JDK versions.

About parameter settings do not take effect in JDK11

This parameter does fail under certain circumstances, but before explaining that, I would like to explain what Min/MaxHeapFreeRatio really does. This parameter acts directly on Old Gen, and is used to control the proportion of the remaining space in the old generation in the entire old generation, rather than the proportion of the remaining space in the entire heap in the entire heap!

I tried to explain in detail with a diagram. The following diagram is a structure diagram of ParallelGC heap memory:

enter image description here

Use Size: Refers to the space occupied by surviving objects in the old gen after GC. Free Size: is the current remaining unused space in the old gen

Through the following formula, we can calculate the proportion of the remaining space in the old generation to the total old generation space.

HeapFreeRatio = (Free Size) / (Old Gen Size) * 100

ParallelGC makes HeapFreeRatio meet the Min/MaxHeapFreeRatio requirement by dynamically adjusting the size of Old Gen after GC.

Old Gen Size = Free Size + Use Size

special case

When the Use Size is too large, Min/MaxHeapFreeRatio will indeed fail.

As the Use Size becomes larger, ParallelGC has to continue to meet the requirements of Min/MaxHeapFreeRatio by expanding the Old Gen. And because Current Heap Size = Young Gen + Old Gen
, so expanding the size of the Old Gen will make the Current Heap Size expand, but the Heap Size has a maximum upper limit. So the following picture will appear (Current == Max):

enter image description here

At this time, ParallelGC can no longer meet the requirements of Min/MaxHeapFreeRatio by expanding Old Gen, but if the Use Size continues to grow, the following situation will occur:

enter image description here

In the above case, the Use Size is too large and the Free Size becomes smaller, so that (Free Size / Old Gen) * 100 < MinHeapFreeRatio. At this point, there will be a phenomenon that does not match your expectations.

in conclusion

Min/MaxHeapFreeRatio is a soft limit, when Current Heap Size < Max Heap Size, ParallelGC can guarantee HeapFreeRatio
fulfil requirements. But when Current Heap Size == Max Heap Size, ParallelGC cannot guarantee that HeapFreeRatio meets the requirements. If you still have doubts about this, I hope to communicate with you further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文