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.

3
  • 5
    How were you intending to launch the same program multiple times in the same JVM? Commented Apr 5, 2013 at 6: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. Commented Apr 5, 2013 at 6:34
  • @ Jon Skeet, I did mean in "different instances of the same JVM" Commented Apr 5, 2013 at 8:09

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is not per class since §3.10.5 of Java Specification Language 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?
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.
2

Same memory location:

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

Distinct memory location:

String s1="Hola"; String s2=new String("Hola"); 

4 Comments

How is this related to my question?
Is this your question?: When do (not) two strings with same content share the same memory?
OMG, you are right but that was simply a sequel title borrowed from the parent topic and your code snippets were used there (anf through referenced there topics) intensively. So, I expected the answerer to read the referenced topic without repeatitions. I removed my downvote though, sorry
Don't worry, that title made me confuse :)
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

3 Comments

The implied question was whether StringConstantPool is created per application or shared between them
You mean per instance of JVM or per (all instances of) the same JVM?
JVM checks the String constant pool first and if the string does not exist, it creates a new String object “Java” and a reference is maintained in the pool. The variable ‘s1′ also refers the same object.
0

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

6 Comments

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, thanks. Because it is obvious for different virtual or physical machines
@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.
@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, 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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.