views:

56

answers:

4

Do we reduce memory consumption when storing a String value that we use very frequent ?

As far as I know, every time we do a "some string" declaration, a new string is constructed, instead of using the address of an existing one with the same value.

Am I correct ?

(I am not talking here about improving the code maintainability)

+2  A: 

.NET uses a string intern pool to store string.

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

Example below shows that the intern pool is used for literal string only. (s2 doesn't reference the same string as s1 even if the content is the same)

string s1 = "MyTest"; 
string s2 = new StringBuilder().Append("My").Append("Test").ToString(); 
string s3 = String.Intern(s2); 
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.

Java does the same thing :

All literal strings and string-valued constant expressions are interned.

madgnome
Java does the same thing with strings as well. http://download-llnw.oracle.com/javase/6/docs/api/java/lang/String.html has the line "All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification"
birryree
Java also interns string literals: "String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern." -- Java Language Specification 3rd Edition, §3.10.5 http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5
R. Bemrose
Thanks @kuropenguin and @R. Bemrose. I've updated my answer!
madgnome
Why don't s1 and s2 reference the same address ?
pigola
@pigola: Because searching the string intern table every time a string is generated is too expensive. Also most generated strings are temporary while interned strings are forever (never GC'd)
Zan Lynx
so when does the search actually occur then ? only when having static or final members ?
pigola
Interesting link on that : http://broadcast.oreilly.com/2010/08/understanding-c-stringintern-m.html
madgnome
+1  A: 

I wouldn't sweat it unless you can identify a genuine performance / memory issue you are trying to address. If you absolutely must optimize, then Javamex has a useful article showing some tips on how to save memory, such as by using Java's internal string pool.

locka
A: 

In Java, you're totally wrong. Indeed, like in .net (or at least as @madgnome says), there is a constant string pool. Notice that, additionnaly to all constant strings, you can push a string to this pool by calling String.intern(). But use this method with cares, as it may be slow due to nature of that pool.

Riduidel
+1  A: 

From the api-doc for the java 2 sdk:

A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

So there is pooling...

The article here makes some good points about it.

hackbert