Skip to content

Instantly share code, notes, and snippets.

@misTrasteos
Last active December 21, 2021 17:42
Show Gist options
  • Save misTrasteos/225ca2f29996a20f88c8ddf0be3cd3b2 to your computer and use it in GitHub Desktop.
Save misTrasteos/225ca2f29996a20f88c8ddf0be3cd3b2 to your computer and use it in GitHub Desktop.
String deduplication Example in Java
jbang run --java-options="-XX:-UseStringDeduplication" https://gist.github.com/misTrasteos/225ca2f29996a20f88c8ddf0be3cd3b2#file-stringdeduplicationexample-java

Same number of String and char[] objects

imagen

jbang run --java-options="-XX:+UseStringDeduplication" https://gist.github.com/misTrasteos/225ca2f29996a20f88c8ddf0be3cd3b2#file-stringdeduplicationexample-java

char[] objects are being reused

Using String deduplication you can allocate much more String objects.

imagen

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA_OPTIONS -Xms128m -Xmx128m
//JAVA_OPTIONS -XX:+UseG1GC
//JAVA_OPTIONS -XX:+AlwaysTenure
//JAVA_OPTIONS -XX:StringDeduplicationAgeThreshold=1
//JAVA_OPTIONS -XX:+HeapDumpOnOutOfMemoryError
//DEPS org.apache.commons:commons-lang3:3.12.0
import org.apache.commons.lang3.RandomStringUtils;
import java.util.List;
import java.util.LinkedList;
public class StringDeduplicationExample {
public static void main(String... args) {
List<String> strings = new LinkedList<String>();
while( args.length >= 0 )
strings.add( RandomStringUtils.randomNumeric(2) );
System.out.println( strings.size() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment