Am I interning my strings correctly?

I want to make sure that I don't beat up the permento space, so I carefully intern my lines.

Are these two statements equivalent?

String s1 = ( "hello" + "world" ).intern(); String s2 = "hello".intern() + "world".intern(); 

UPDATE

As I formulated my question, it was completely different from the real application. Here is the method in which I use intern.

 public String toAddress( Transport transport ) { Constraint.NonNullArgument.check( transport, "transport" ); switch( transport ) { case GOOGLE: case MSN: return ( transport.code() + PERIOD + _domain ).intern(); case YAHOO: default: return _domain; } } private String _domain; // is initialized during constructor private static final String PERIOD = "."; 
+4
source share
9 answers

The best advice I can think of: don't worry. A statically declared String will be in the constant pool in some way unless you dynamically create a String that ... errr no I can't think of a reason.

I programmed using Java with 97, and I never used String.intern() .

EDIT: after watching your update, I really think you should not use intern (). Your method looks perfectly normal and there is no reason to use intern ().

My reason is that it infects the optimization, and possibly premature, you already guess the garbage collector. If the method just of you is short-lived, then the resulting line will die the young generation very soon after that in the next junior GC, and if it does not, it will be interned (due to a better word) in the mature generation anyway.

I think the only time this may be a good idea is that you spend a little time on the profiler and prove that it greatly affects the performance of your application.

+10
source

As jensgram says, these two statements are not equivalent. Two important rules:

  • Concatenation of string literals in code ends with a string constant, so these two operators are exactly equivalent (they will create the same bytecode):

     String x = "foo" + "bar": String x = "foobar"; 
  • String constants are interned automatically; you do not need to do this explicitly

Now, this focuses on literals — do you actually call intern literals, or is your real use case somewhat different (for example, interning values ​​obtained from a database that you often see)? If yes, please tell us more details.

EDIT: Okay, based on editing the question: it can save some memory if you end up storing the return value toAddress() somewhere, that it will remain in place for a long time, and you will end up in the same address several times. If this is not the case, internment is actually likely to make matters worse. I do not know for sure whether the interned strings will remain forever, but this is entirely possible.

It seems to me that this is unlikely to be a good use of internment, and most likely it will be worse. You mention that you are trying to save the space of the pergman - why do you think that interning there will help? Concatenated strings will not end up anyway, if I'm not mistaken.

+10
source

No. Adding two interned strings together does not give you an interned string.

However, it is quite rare that you need to "carefully put one line." If you are not dealing with a huge number of identical lines, this is more of a problem than it costs.

+7
source

I would say no. s1 adds "helloworld" to the pool, while s2 consists of two concatenated strings "hello" and "world".

+2
source

Additional information will help us understand your request ... Anyway ...

If you want a trainee for HelloWorld manually, then go on to the first statement, as in the second statement, which you will internally greet and greet separately. The two statements are not identical at all.

+2
source

You might want to have some form of proof (perhaps through profiling) that you “beat up the permento space” before writing all your code.

Otherwise, you can simply do "premature optimization", which is generally not approved. See http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize for more details on why this might be bad.

+2
source

In many cases, "thoroughly interning" your lines gives you nothing but some time. Consider the following case:

 void foobar(int x) { String s1 = someMethod(x).intern(); ... ... } 

So s1 is interned, no (heap) of space wasted? Wrong! Most likely, the intermediate result of someMethod (x) still exists somewhere on the heap and needs to be garbage collected. This is because someMethod () somehow built the string, and (unless it returns a literal), it did this on the heap. But then ... better take a look at what the space of pergens is used for. It is used for metadata about classes and (ooops) of the String.intern table. By interning all your lines, you do exactly what you wanted to avoid: Pummel the permgen space.

Further information here: http://www.thesorensens.org/2006/09/09/java-permgen-space-stringintern-xml-parsing/

0
source

The number of lines you use does not affect the constant generation of JVMs, since we are still talking about the same class.

0
source

interning strings are basically a memory leak waiting for the following: (

If you have a very, very good reason [1], do not do this, but leave it in the JVM.

[1] Like in the section "Dear Boss, please do not shoot me. I have profiling data to support my decision to use an intern" :)

0
source

All Articles