String Interning and Performance Tuning in Java

String interning is a method of storing only one copy of each distinct string value. Strings in Java are immutable and hence this sharing is perfectly safe and give you better performance. The distinct values are stored in a fixed-size hashtable usually referred to as string intern pool or string pool. The single copy of each string is called its 'intern'. You can read more about the basics of String interning with examples @ string-interning-in-java-with-examples.

The table of interned strings is held in native memory (not heap memory) as a fixed-size hashtable. In releases prior to Java 7u40, the default size of the table is 1,009 buckets. In 64-bit versions of Java 7u40 and later, the default size is 60,013. This table should be sized according to the expected data for better performance. Starting in Java 7, the size of this table can be set when the JVM starts by using the flag:

-XX:StringTableSize=<value>

If an application will intern a lot of strings, this number should be increased. And when you set it, make sure that the value is a prime number for best efficiency.

 

Ideal bucket size

What is a bucket? A hashtable data structure contains an array that can hold some number of entries and each element in the array is called a bucket. In simple terms, the bucket location is calculated using a hashing algorithm. Two elements can have the same value for hashing algorithm, and this situation is called collision. A collision is usually handled by creating a linked list at each array location (bucket) and adding every element that hashes to that bucket to that linked list. Read more about hashing for more clarity.

Ideally, the average bucket size should be 0 or 1 which means there are no collisions, and hence better performance. If the averages are larger than 1, we can increase the string table size accordingly. Each bucket takes only 4 or 8 bytes (depending on whether you have a 32- or 64-bit JVM). So there won’t be any notable penalty for setting the size of the string table too high.

 

String interning statistics

To see the details of your string table, run your application with the flag

-XX:+PrintStringTableStatistics

This flag requires JDK 7u6 or later and default value is false. When the JVM exits, it will print out a table with details such as number of buckets, average bucket size, variance of bucket size, std. dev. of bucket size and maximum bucket size. This command also displays information about the symbol table. The symbol table is used to hold some class information. This flag is not generally tunable, but JDK 8 has an experimental flag to adjust the size of that table.

The number of interned strings an application and their total size can also be obtained using the jmap command as:

jmap -heap <process_id>

Within the output, look out for ‘interned Strings occupying’ phrase as:

3000 interned Strings occupying 300000 bytes

Like PrintStringTableStatistics, this also requires JDK 7u6 or later.

 

Summary

In summary, applications that reuse the same strings a lot will benefit by interning those strings. Applications that intern many strings may need to adjust the size of the string intern table accordingly. 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)