0

In the code snippet below, new String object will be created to to store the modified new string as Strings are immutable in java. But i'm not sure which one will create new object and which one of them will be marked for garbage collection?

String s1 = "It";
String s2 = "was";
String s3 = s1+" "+s2;
s2+=" roses";
s3 = s3+s2+" roses all the way";
System.out.println(s3);
IAmBlake
  • 457
  • 6
  • 21

2 Answers2

0

It depends on scope of your code snippet.

If all code is inside one method all of them will be in garbage after execution.

Garbage Collector works on count of references to the object.

As example you declare a new reference String s1 inside a method and assign to something. Then method executed and upon completion there is no more references. So, go to garbage.

Vadim
  • 4,027
  • 2
  • 10
  • 26
  • That's misleading: 1. There's *reference counting* which is (let's say) a possible implementation of *garbage collection*, but I've never heard about it being used for Java (probably as it can't deal with cycles). The used garbage collectors have no idea about the exact reference count; they only know referenced or not. 2. Upon completion of the method, `"It"` has lost all references, but nobody cares as it's a member of the literal pool. 3. There's no explicit GC action taken when a method exits (you didn't claim the opposite, but that's how I'd understand it). – maaartinus Jan 23 '17 at 00:34
  • @maaartinus - I guess you are right. But "count of reference" or "is referenced" - what's a difference? count==0 means unreferenced :-). I never paid too much attention to it especially for strings, because in real "who cares?" :-). I just had some cases when I had to set reference to null explicitly to make GC destroy objects (not strings) earlier. ... long time back... – Vadim Jan 23 '17 at 13:50
0

This is a brief, basic explanation, the actual behavior can vary based on compiler and JVM used. Also there are a lot of articles available that go into this topic in depth and can provide more detailed explanations.

These literals will be put in the JVM's string pool and never be GC'ed. (i.e. they will exist in memory for the duration of the JVM.

"It", "was", " ", " roses", " roses all the way"

Then, as far as the String references are concerned, It depends on the scope of the variables. I will assume local method level for this answer:

String s1 = "It"; // a reference will be created on the stack 

String s2 = "was"; // a reference will be created on the stack 

String s3 = s1+" "+s2; // a reference will be created on the stack for s3, and then two temp objects will be created in memory, one for s1+" ", one for concatenating the result with +s2.  (this operation can vary greatly based on how compiler optimizes),  the first one will become eligible for GC immediately.

s2+=" roses"; // same as above.

s3 = s3+s2+" roses all the way"; // same as above but the object s3 was pointing to will become eligible for GC immediately.

System.out.println(s3); // no memory allocation.

When the method ends, s1, s2, and s3 references will be cleared from the stack and any remaining objects pointed to become eligible for GC.

Hope this helps, remember this is a very basic explanation, I recommend reading up on this topic, as how it will actually behave can vary greatly depending on how the compiler decides to optimize. (For example, the compiler may see all these temporary references and concatenations are not needed and discard them)

Note: Since you are concatenating to create strings, you may want to consider the Mutable alternatives like StringBuffer or StringBuilder which can help optimize.

Bob Lukens
  • 700
  • 6
  • 10