IntelliJ Java IDE
The moose likes Performance and the fly likes String intern(); Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "String intern();" Watch "String intern();" New topic
Author

String intern();

Ron Perich
Ranch Hand

Joined: Mar 25, 2003
Posts: 41
I have recently learned how the inter() method can be very useful in controlling the amount of memory used by the JVM. However, there is overhead in using the method, and it would not make sense in all sitations.
Do you regularly use the intern() method, and if so, when do apply it?
Thanks for the input!


RP
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I am aware of that method and it's applicability for some time now, but I never used it yet.
I would also be interested in some case studies with some hard numbers on the effect of using String.intern().
[ April 30, 2003: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ron Perich
Ranch Hand

Joined: Mar 25, 2003
Posts: 41
I think it's a great idea, and I can see how it would help, but the problem is you have to know when it needs to be used.
If you don't call intern() and save the result, you'll never see the benefits. I'd like some insight on how to use it effectively
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11945
Would someone shed some light on how String.intern() improves performance?


Author of Test Driven (Manning Publications, 2007) [Blog] [HowToAskQuestionsOnJavaRanch]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
2 ways that I know:
If you're storing a lot of Strings in memory, and many of them are really duplicates of each other, you can use intern() to remove the duplicates. Given a reference str:
str = str.intern();
If there is some other string already interned with the same value as str, this code will replace str with a reference to the previously interned copy. Then the object str originally referenced can be GC'd. This can reduce memory use, which may well improve performance.
Secondarily, if your code is spending a lot of time comparing strings with the equals() method, you may be able to reduce this by ensuring that all strings to compare have been interned, and then use == rather than equals(). (Because for interned strings, these are equivalent.) Note that intern() itself takes some time to execute, probably more than several invocations of equals() - so this strategy only makes sense if you're going to compare to a lot of different strings. Consider it if you see code like this:

You can replace this with

Note that the string literals are already interned. Basically the initial intern() slows you down, but each subsequent == is faster. So if there are enough comparisons made, this performs better. Dunno what the threshold is (i.e., how many comparisons?) for when the intern approach makes more sense; best to test I suppose. And note that most applications with a structure like the above could also benefit from being rewritten to use a HashMap to look up a command object instead. So YMMV and all that. I don't think it's very common for an application to benefit much from this particular pattern (replaceing equals() with ==), but I could be wrong.
[ April 30, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
 
subject: String intern();
 
Threads others viewed
intern() in String
Strings
String.intern()
intern method
what's intern() in strings
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture