sequence
This article focuses on String Deduplication for the G1 GC
-XX:+UseStringDeduplication
- The JDK8U20 brings the String Deduplication feature to the G1 GC to point the same String to the same data, reducing the memory overhead of duplicate strings
- This feature is turned off by default and can be enabled using -xx :+UseStringDeduplication.
The premise is to use -xx :+UseG1GC
) - The JVM will record the weak reference and hash value of a char[]. When a String with the same hash code is found, the JVM will compare the weak reference and hash value of a char[]. Then one of the strings modifies the pointer to the char[] of the other String so that the char[] of the former can be reclaimed
The instance
The experimental code
@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); }Copy the code
Close the StringDeduplication
-XX:+UseG1GC -XX:-UseStringDeduplication
Copy the code
The output is as follows:
string list size inMB: 586.8727111816406 informs the heap sizein MB:831.772346496582
used non heap size inMB: 6.448394775390625Copy the code
- The entire JVM heap takes up about 831MB, of which string list takes up about 586MB
Open StringDeduplication
-XX:+UseG1GC -XX:+UseStringDeduplication
Copy the code
The output is as follows:
string list size inMB: 296.83294677734375 informs the heap sizein MB:645.0970153808594
used non heap size inMB: 6.376350402832031Copy the code
- The entire JVM heap takes up about 645MB, of which string List takes up about 296MB
summary
- The JDK8U20 brings the String Deduplication feature to the G1 GC to point the same String to the same data, reducing the memory overhead of duplicate strings
- This feature is turned off by default and can be enabled using -xx :+UseStringDeduplication.
The premise is to use -xx :+UseG1GC
) - Enabling String Deduplication with the G1 GC does save some memory — about 20 percent — when there are a lot of duplicate strings, but this is ideal because there may not be many duplicate strings in normal applications
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%