简体   繁体   中英

String Immutability memory Issue

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.?

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.

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).

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.

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)) .

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.

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM