1

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?

Jonathan Lam
17.5k17 gold badges72 silver badges99 bronze badges
asked Aug 14, 2019 at 16:21
2
  • 3
    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 Commented Aug 14, 2019 at 16:29
  • 1
    @zabuza - your comment is the best answer for the question so far.. Commented Aug 14, 2019 at 17:43

5 Answers 5

6

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.

answered Aug 14, 2019 at 16:25

3 Comments

Technically they don't get "marked" at the end of the loop, they simple become "eligible for GC" at that time. The "marking" happens during the GC run.
Fixed that item.
As long as no actual use of 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.
3

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.

answered Aug 14, 2019 at 16:26

1 Comment

Assignments are not only stored in stack memory. They are also in heap memory (field assignment).
1

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

answered Aug 14, 2019 at 16:26

Comments

0

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.

answered Aug 14, 2019 at 16:54

Comments

0

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.

MemoryTest class memory usage

answered Aug 18, 2019 at 10:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.