What i would like to ask is this:
String str1;
for(int i=0;i<10;i++){
str1 = Integer.toString(i);
}
will this create 1 string object and reassign it's value 10 times or will it create 10 String Objects eating 10*(String's Bytes)
from memory?
5 Answers 5
That will create 10 different string objects, each assigned to the str1
variable in turn. All but the final one (the one currently referenced by str1
) will be available for garbage collection at the end of the loop.
3 Comments
str1
has been placed after the loop, all ten strings are eligible for garbage collection. It depends on several different surrounding conditions whether the tenth would actually get collected when a gc happened right after the loop, but formally, it is eligible and sometimes, it happens in practice. Though, after the hot spot optimizer looked at the code, it might also happen that none of these strings (or only the last one) is ever created.There are different concepts at play:
Assignments are stored in stack memory.
Objects are stored in heap space
Because integers and strings are immutable, each time you do Integer.toString()
an instance of string will be created, so you will have 10 instances of String in heap.
on each loop run, you are telling str1
to point at each of those specific instances, but it is replaced immediately by the next run.
Garbage collector will check eventually which heap objects do not have a stack memory reference and collect them to throw them into oblivion.
1 Comment
It will create 10 of them. String is actually immutable. But the garbage collector will take care of all that if needs be.
You can see that in the doc ;)
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Comments
In java String is immutable, by immutable, we mean that Strings are constant, their values cannot be changed after they are created.
from specification
a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions - are "interned" so as to share unique instances, using the method String.intern.
in this case it will create 10 immutable string objects.
Comments
A lot of answers have been provided and I won't add to them anymore. Moving forward, if you are interested into how the JVM memory management works, I would suggest to checkout Java Memory Management course by Matt Greencroft, https://www.udemy.com/java-memory-management/. I bought it 2 years ago during a sale and it was worth it.
I ran your code in an infinite loop with a 5 secs. pause.
class MemoryTest {
public static void main(String[] args) throws InterruptedException {
while (true) {
String str1;
for (int i = 0; i < 10; i++) {
str1 = Integer.toString(i);
}
Thread.sleep(5000);
System.out.println("Running...");
}
}
}
This is the result of Visual VM + Visual GC plugin, thanks to the course. I have been running it for 12 mins. and only 1 garbage collection has been executed by the JVM. Even in an infinite loop, the JVM is smart enough to keep the memory in S1 space and never execute more than one GC and never put the object in Old space.
str1
is a variable, not a string object. so u have one variable and u create 10 string objects and then constantly reassign the one variable. after the loop, all but the last string object are marked for the garbage collector and will eventually be freed