Java8 new feature string to reintroduce

  • 2020-04-01 03:28:30
  • OfStack

On August 19, Oracle released JDK 8u20, which contains many new features, such as Java compiler updates, support for API modification of MinHeapFreeRatio and MaxHeapFreeRatio parameters at runtime, and new GC tuning guidelines. But the most anticipated of the new features is String Deduplication. How to reduce the memory footprint has always been an eternal topic, and in Java applications, you will often see the String object, one of the most commonly used objects in Java, taking up 30% of the application's memory. The new String deduplication feature, which currently only works with the G1 garbage collector and is not turned on by default, can help reduce the memory footprint of String objects in your application.

Fabian Lange explains how to implement the string deduplication feature:


The garbage collector will be accessing String Object to mark its array of characters and String The hash value and the weak reference are stored in an array. When the garbage collector finds another with the same hash value String Object, it matches the two objects character by character. If they match exactly, then one of them String It will be modified to point to another String Array of characters. Since the first character array is no longer referenced, it can be recycled. The garbage collector minimizes the overhead of the entire operation, such as one String The object scan does not detect duplication, so it will not be checked again for a period of time.

Next, Fabian Lange explains the magic of string de-duplication in code. First, use Java 8 Update 20 to run the following code through the parameter -xmx256m-xx :+UseG1GC:


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 up running after 30 loops due to an OutOfMemoryError exception. In the use of parameter - XX: + UseStringDeduplication - XX: + PrintStringDeduplicationStatistics open string to heavy features, the program can run for a period of time. The JVM logs also provide detailed information about the entire deduplication process. Please test yourself.

Finally, Fabian Lange explains the difference between String de-duplication and String hosting, which are similar, except that the String hosting reuses the entire String instance, and the String de-duplication is just an array of characters for strings.

(full text)


Related articles: