On August 19, Oracle released JDK 8u20. JDK 8u20 includes many new features, such as Java compiler updates, support for modifying MinHeapFreeRatio and MaxHeapFreeRatio parameters through APIs at runtime, and new GC tuning guide documents. However, among the many new features, the most anticipated one is String Deduplication. How to reduce memory usage has always been an eternal topic. In Java applications, it is often seen that the String object takes up 30% of the application's memory. It is one of the most commonly used objects in Java. The new string deduplication feature can help reduce the memory footprint of String objects in applications. Currently, this feature is only available for the G1 garbage collector and is not enabled by default.
Fabian Lange explains how string deduplication is implemented:
Copy the code code as follows:
The garbage collector will mark the character array of the String object when it is accessed, and save the hash value of the String and the weak reference into an array. When the garbage collector finds another String object with the same hash value, it compares the two objects character by character. If they match exactly, one String will be modified to point to the character array of the other String. Since the first character array is no longer referenced, it can be recycled. The garbage collector will try to reduce the cost of the entire operation. For example, if a String object is scanned and no duplicates are found, it will not be checked again in the next period of time.
Next, Fabian Lange explained the magical effect of string deduplication through code. First run the following code using Java 8 Update 20 with parameters -Xmx256m -XX:+UseG1GC:
Copy the code code as follows:
public class LotsOfStrings {
private static final LinkedList<String> LOTS_OF_STRINGS = new LinkedList<>();
public static void main(String[] args) throws Exception {
int iteration = 0;
while (true) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 1000; j++) {
LOTS_OF_STRINGS.add(new String("String " + j));
}
}
iteration++;
System.out.println("Survived Iteration: " + iteration);
Thread.sleep(100);
}
}
}
The code will end running due to OutOfMemoryError exception after 30 loops. After using the parameters -XX:+UseStringDeduplication -XX:+PrintStringDeduplicationStatistics to enable the string deduplication feature, the program can run for a longer period of time. You can also learn more about the details of the entire deduplication process through the JVM logs. Readers are asked to test it themselves.
Finally, Fabian Lange also explained the difference between string deduplication and string residency. They are very similar, except that string residency reuses the entire String instance, while string deduplication only targets the character array of String.
(Full text ends)