Re:JVM Anatomy Quark #10: String.intern (2019)
by Someone on Wednesday June 22, 2022 @04:11PM
(#31841264)
I never understood it, either. It would be useful (at least historically) in cases where you’re willing to give up performance in exchange for lower memory usage, and then, you’d still have to make good choices on what strings to intern and what strings not to intern (I think the standard example is a XML parser that may choose to intern all node and attribute names of a XML schema. If you’re parsing a large file using a DOM parser, that can significantly decrease memory usage)
Problem 1: it can easily depend on your input data whether interning is necessary to prevent out of memory exceptions.
Problem 2: there often are better solutions to memory usage than interning strings (for example, by replacing that DOM parser by a streaming one)
Problem 3: if you’re writing a library, you can’t know anything about the application that will use your code, so you have to guess whether interning some of the strings you create will help the user.
And of course, that’s historically. (Some) modern JVMs already deduplicate strings.
Problem 1: it can easily depend on your input data whether interning is necessary to prevent out of memory exceptions.
Problem 2: there often are better solutions to memory usage than interning strings (for example, by replacing that DOM parser by a streaming one)
Problem 3: if you’re writing a library, you can’t know anything about the application that will use your code, so you have to guess whether interning some of the strings you create will help the user.
And of course, that’s historically. (Some) modern JVMs already deduplicate strings.