• Post Reply Bookmark Topic Watch Topic
  • New Topic

String intern();

 
Ron Perich
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ron Perich
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would someone shed some light on how String.intern() improves performance?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!