4

Once the String object is created , we can't modify it But if we do any operations on it JVM will create New Object. Here by creating new objects then JVM consumes more memory. Then i think it causes to memory issue right.?

1
  • 1
    If you want something mutable you can use StringBuilder instead. Commented Apr 26, 2012 at 11:22

9 Answers 9

3

You are correct. It is definitely worth being aware of this issue, even if it doesn't affect you every time.

As you say, Strings cannot change after creation - they're immutable and they don't expose many ways to change them.

However, operations such as a split() will be generating additional string objects in the background, and each of those strings have a memory overhead if you are holding onto references to them.

As the other posters note, the objects will be small and garbage collection will usually clean up the old ones after they have gone out of scope, so you generally won't have to worry about this.

However, if you're doing something specific and holding onto large amounts of string references then this could bite you.

Look at String interning depending on your use case, noting the warnings on the linked page.

Two things to note:

1) Hard coded String literals will be automatically interned by Java, reducing the impact of this.

2) The + operator is more efficient in this regard, it will use String Builders underneath giving performance & memory benefits.

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

Comments

2

No, that does not. If you do not hold strong links to String instances they eventually will be collected by a garbage collector.

For example:

while (true) {
    new String("that is a string");
}

in this snippet you continuously create new object instances, however you will never get OutOfMemoryException as created instances become garbage (there are obviously no strong links).

4 Comments

this is not really a good example because (a) strings are rarely created using new String (b) you're not modifying your string like the OP says.
@dogbane, are there any constructive difference between String instance created with new String() and String instance created internally after modification?
Plus in this example Java would automatically intern the String literal so its not a representative example - stackoverflow.com/questions/1855170/…
@BenjaminWootton, only string literal will be interned. Created object will not.
1

It consumes more memory for new objects, that's right. But that fact in itself does not create an issue, because garbage collector promptly reclaims all inaccessible memory. Of course you can turn it into an issue by creating links to the newly created strings, but that would be an issue of your program, not of JVM.

Comments

1

The biggest memory issue you have to know about is taking a small substring of a huge string. That substring shares the original string's char array and even if the original string gets gc'd, the huge char array will still be referenced by the substring. The workaround is to use new String(hugeString.substring(i)).

Comments

0

The issue that is generated is the fact that garbage is generated. This issue is resolved by the virtual machine by calling the garbage collector which frees the memory used by that garbage.

Comments

0

As soon as the old object is not used anymore, it can be removed by the garbage collector. (Which will be done far before any memory issue arises).

If you want to prevent the copying of the data, use a StringBuilder.

Comments

0

Unused objects are collected by GC.

and Immutability got many benefits in java.

In Java achieving as much immutability as possible is a good practice.

They can be safely used in Collections frameworks also.

Check this

2 Comments

You are making too broad generalizations. Immutability is useful in some places while mutability is useful in other places.
that is agreed I never said you have to use immutability in all places,I only said it has got many benefits and usable in Collections.even Joshua bloch said it in Effective java
0

As far as I know StringBuilder (or StringBuffer for thread safe) is useful for managing String and make them mutable.

Manipulate some characters in a huge String do not 'eat' many bytes in memory.

It is also more powerful/speed for concate.

Comments

0

Since a string instance is immutable it can be reused by the jvm. The String class is implemented with Flyweight Design Pattern that is used to avoid memory issues.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.