3

Coming from the question will two strings with same content be stored in the same memory location?

Having the Java code

String s1="Java";

will this string be allocated in the same memory location (or multipe):

  • if to launch the same program multiple times executing it in parallel (concurrently)?

Possible answer:

I am currently C# developer (though programmed in Java in the previous millennium).

I asked this question because I believed it is the same between .NET CLR and Java (JVM) and I was hoping to get the answer for .NET apps (but somehow was in doubt by frequently encountered "application" pool terms).

So, it seems to be (sorry for not exhaustively searching before asking):

with the answer that string intern pool is shared per all instances/programs of the same JVM or .NET CLR.

Community
  • 1
  • 1
  • 5
    How were you intending to launch the same **program** multiple times in the same JVM? – Jon Skeet Apr 05 '13 at 06:34
  • 1
    Well, I doubt it will ever be allocated in the same memory location with each successive execution because that is entirely dependent upon what memory locations are available to the application at the time of execution. This is true for any language as they all, ultimately, access the computers memory through the OS. – Brandon Buck Apr 05 '13 at 06:34
  • @ Jon Skeet, I did mean in "different instances of the same JVM" – Gennady Vanin Геннадий Ванин Apr 05 '13 at 08:09

4 Answers4

2
Is string pool created per class basis or per JVM basis?

There is one String pool per JVM ... unless you are using some exotic JVM where they've decided to implement it differently.

I think that answers it, right?

from similar question

Community
  • 1
  • 1
d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
  • It is not per class since [§3.10.5 of Java Specification Language](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5) tells that it is shared between multiple packages "Literal strings within different classes in different packages likewise represent references to the same String object". So, all instances of JVM or applications launched from the same JVM share the same pool? – Gennady Vanin Геннадий Ванин Apr 05 '13 at 08:30
  • The answer to the question above answers more than just the question... Yes, the question asks about classes - but the answer seems to go further to imply that the JVM has the string literal pool and all apps within it share the pool. That's what I inferred anyway. – d'alar'cop Apr 05 '13 at 08:35
2

Same memory location:

String s1="Hola";
String s2="Hola";

Distinct memory location:

String s1="Hola";
String s2=new String("Hola"); 
Waji
  • 71
  • 3
1

@gennady-vanin-novosibirsk....

String s1="Java";String s2="Java";String s3="Java";String s4="Java";

  1. The above all objects available in StringConstantPool location those are all objects are pointing to only one ("Java") location
Srinivas
  • 147
  • 5
0

Strings of same contents will share same space for every instance of same JVM

Mohan Raj B
  • 1,015
  • 7
  • 14
  • 1
    I presume you mean "different JVM *instance*", took liberty of editing your answer, feel free to edit further or rollback. The JVM software installation itself can be (and usually is) same. – hyde Apr 05 '13 at 07:13
  • @Hyde, thanks. Because it is obvious for different virtual or physical machines – Gennady Vanin Геннадий Ванин Apr 05 '13 at 08:07
  • @Hyde, thanks. But you should post it as answer. Because your edit reverted the answer to my question to opposite and all other answers imply that string pool is shared between all instances of the same JVM, i.e. between all applications launched from the same installed JVM. – Gennady Vanin Геннадий Ванин Apr 05 '13 at 08:36
  • @GennadyVanin--Novosibirsk I think all the answers mean a JVM instance, when talking about "JVM", and do not imply interned strings are shared between instances... How would that even work efficiently and portably? Anyway, I'll leave it for MohanRajB to roll back, if I misinterpreted. – hyde Apr 05 '13 at 08:42
  • @hyde, please see my Update in body of question. I believe you should revert your edit though, honestly, I was myself confused honestly thinking that intern pool is per instance of JVM or per app – Gennady Vanin Геннадий Ванин Apr 05 '13 at 09:13
  • @GennadyVanin--Novosibirsk Very well, I did rollback. – hyde Apr 05 '13 at 09:30