gpt4 book ai didi

java - G1 垃圾收集器是否默认启用了字符串去重功能?

转载 作者:太空狗 更新时间:2023-10-29 23:03:06 28 4
gpt4 key购买 nike

JEP 192: String Deduplication in G1在 Java 8 Update 20 中实现添加了新的字符串重复数据删除功能:

Reduce the Java heap live-data set by enhancing the G1 garbage collector so that duplicate instances of String are automatically and continuously deduplicated.

JEP 页面提到命令行选项 UseStringDeduplication (bool) 允许启用或禁用重复数据删除功能。但是 JEP 页面并没有显示默认值。

➠ 在与 Java 8 和 Java 9 捆绑在一起的 G1 垃圾收集器中,默认情况下,去重功能是开启还是关闭?

➠ 是否有“getter”方法在运行时验证当前设置?

除了 JEP 页面,我不知道去哪里寻找文档。

至少在HotSpot -Java 9 的实现,G1 garbage collectorenabled by default .这个事实现在提示了这个问题。有关字符串驻留和重复数据删除的更多信息,请参阅 this 2014-10 presentation by Aleksey Shipilev 29:00。

最佳答案

字符串去重默认关闭

对于下面看到的 Java 8 和 Java 9 版本,UseStringDeduplication 默认为 false(禁用)。

一种验证功能设置的方法:list out all the final flags for JVM然后寻找它。

构建 1.8.0_131-b11

    $ java -XX:+UseG1GC  -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep -i 'duplicat'
bool PrintStringDeduplicationStatistics = false {product}
uintx StringDeduplicationAgeThreshold = 3 {product}
bool StringDeduplicationRehashALot = false {diagnostic}
bool StringDeduplicationResizeALot = false {diagnostic}
bool UseStringDeduplication = false {product}
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

构建 9+18

    $ java -XX:+UseG1GC  -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep -i 'duplicat'
uintx StringDeduplicationAgeThreshold = 3 {product} {default}
bool StringDeduplicationRehashALot = false {diagnostic} {default}
bool StringDeduplicationResizeALot = false {diagnostic} {default}
bool UseStringDeduplication = false {product} {default}
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

另一种测试方法是用

package jvm;

import java.util.ArrayList;
import java.util.List;

public class StringDeDuplicationTester {

public static void main(String[] args) throws Exception {
List<String> strings = new ArrayList<>();
while (true) {
for (int i = 0; i < 100_00; i++) {
strings.add(new String("String " + i));
}
Thread.sleep(100);
}
}
}

在没有明确指定的情况下运行。

$ java  -Xmx256m -XX:+UseG1GC -XX:+PrintStringDeduplicationStatistics jvm.StringDeDuplicationTester
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at jvm.StringDeDuplicationTester.main(StringDeDuplicationTester.java:12)

在明确打开它的情况下运行。

$ java  -Xmx256m -XX:+UseG1GC -XX:+UseStringDeduplication -XX:+PrintStringDeduplicationStatistics jvm.StringDeDuplicationTester
[GC concurrent-string-deduplication, 5116.7K->408.7K(4708.0K), avg 92.0%, 0.0246084 secs]
[Last Exec: 0.0246084 secs, Idle: 1.7075173 secs, Blocked: 0/0.0000000 secs]
[Inspected: 130568]
[Skipped: 0( 0.0%)]
[Hashed: 130450( 99.9%)]
[Known: 0( 0.0%)]
[New: 130568(100.0%) 5116.7K]
[Deduplicated: 120388( 92.2%) 4708.0K( 92.0%)]
[Young: 0( 0.0%) 0.0B( 0.0%)]
[Old: 120388(100.0%) 4708.0K(100.0%)]
[Total Exec: 1/0.0246084 secs, Idle: 1/1.7075173 secs, Blocked: 0/0.0000000 secs]
[Inspected: 130568]
[Skipped: 0( 0.0%)]
[Hashed: 130450( 99.9%)]
[Known: 0( 0.0%)]
[New: 130568(100.0%) 5116.7K]
[Deduplicated: 120388( 92.2%) 4708.0K( 92.0%)]
[Young: 0( 0.0%) 0.0B( 0.0%)]
[Old: 120388(100.0%) 4708.0K(100.0%)]
[Table]
[Memory Usage: 264.9K]
[Size: 1024, Min: 1024, Max: 16777216]
[Entries: 10962, Load: 1070.5%, Cached: 0, Added: 10962, Removed: 0]
[Resize Count: 0, Shrink Threshold: 682(66.7%), Grow Threshold: 2048(200.0%)]
[Rehash Count: 0, Rehash Threshold: 120, Hash Seed: 0x0]
[Age Threshold: 3]
[Queue]
[Dropped: 0]
[GC concurrent-string-deduplication, deleted 0 entries, 0.0000008 secs]
...
output truncated

<子>注意:此输出来自build 1.8.0_131-b11。看起来 Java 9 没有打印字符串去重统计信息的选项。潜在的错误?。统一日志记录终止了这个特定选项。

$ java  -Xmx256m -XX:+UseG1GC -XX:+PrintStringDeduplicationStatistics -version
Unrecognized VM option 'PrintStringDeduplicationStatistics'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

关于java - G1 垃圾收集器是否默认启用了字符串去重功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46537415/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com