转载

聊聊G1 GC的String Deduplication

本文主要研究一下G1 GC的String Deduplication

-XX:+UseStringDeduplication

前提是使用-XX:+UseG1GC

实例

实验代码

@Test
    public void testG1StringDeduplication() throws InterruptedException {
        List<String> data = IntStream.rangeClosed(1,10000000)
                .mapToObj(i -> "number is " + ( i % 2 == 0 ? "odd" : "even"))
                .collect(Collectors.toList());
        System.gc();
        long bytes = RamUsageEstimator.sizeOfAll(data);
        System.out.println("string list size in MB:" + bytes*1.0/1024/1024);
        System.out.println("used heap size in MB:" + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()*1.0/1024/1024);
        System.out.println("used non heap size in MB:" + ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed()*1.0/1024/1024);

    }

关闭StringDeduplication

-XX:+UseG1GC -XX:-UseStringDeduplication

输出如下:

string list size in MB:586.8727111816406
used heap size in MB:831.772346496582
used non heap size in MB:6.448394775390625
  • 整个jvm heap占用了约831MB,其中string list占用了约586MB

开启StringDeduplication

-XX:+UseG1GC -XX:+UseStringDeduplication

输出如下:

string list size in MB:296.83294677734375
used heap size in MB:645.0970153808594
used non heap size in MB:6.376350402832031
  • 整个jvm heap占用了约645MB,其中string list占用了约296MB

小结

前提是使用-XX:+UseG1GC

doc

  • Java 8 String deduplication vs. String.intern()
  • JEP 192: String Deduplication in G1
  • String Deduplication – A new feature in Java 8 Update 20
  • Java 8 Update 20: String Deduplication
  • Duplicate Strings: How to Get Rid of Them and Save Memory
  • String deduplication feature (from Java 8 update 20)
  • G1 GC: Reducing Memory Consumption by 20%
原文  https://segmentfault.com/a/1190000018810200
正文到此结束
Loading...