Java Reference
In-Depth Information
Table 7-3. Time to intern 10 million strings
Tuning
Time
String table size 1009
2.3 hours
String table size 1 million 30.4 seconds
String table size 10 million 25.2 seconds
Custom method
26.4 seconds
Note the very severe penalty for the improperly sized string intern table. Once the table is
sized according to the expected data, performance is drastically improved.
For that last test case, instead of using the intern() method, I used the sample canonic-
alVersion() method shown previously and implemented with a CustomConcur-
rentHashMap class (from a prerelease version of JSR 166) with weak keys and values. That
didn't help performance against a well-tuned string intern table, but the advantage with the
custom implementation is that I didn't need to tune the size of that custom map at all. The
CustomConcurrentHashMap was given an initial size of 1,009, and it resized itself dynamic-
ally as needed. That had a small penalty against the best-tuned string table size, but it was far
easier to run. (In that case, though, the code had to call the canonicalVersion() method of
the custom class; this isn't a simple replacement for the intern() method.)
To see how the string table is performing, run your application with the -
XX:+PrintStringTableStatistics argument (this flag requires JDK 7u6 or later and is
false by default). When the JVM exits, it will print out a table like this:
StringTable statistics:
Number of buckets : 1009
Average bucket size : 3008
Variance of bucket size : 2870
Std. dev. of bucket size: 54
Maximum bucket size : 3186
This command also displays information about the symbol table, but the string table is what
interests us here. (The symbol table is used to hold some class information. JDK 8 has an ex-
perimental flag to adjust the size of that table, but it is not generally tunable.) In this case,
Search WWH ::




Custom Search