本文主要研究一下G1 GC的String Deduplication
前提是使用-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); }
-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
-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
前提是使用-XX:+UseG1GC