Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does appending "" to a String save memory?

I used a variable with a lot of data in it, say String data. I wanted to use a small part of this string in the following way:

this.smallpart = data.substring(12,18); 

After some hours of debugging (with a memory visualizer) I found out that the objects field smallpart remembered all the data from data, although it only contained the substring.

When I changed the code into:

this.smallpart = data.substring(12,18)+"";  

..the problem was solved! Now my application uses very little memory now!

How is that possible? Can anyone explain this? I think this.smallpart kept referencing towards data, but why?

UPDATE: How can I clear the big String then? Will data = new String(data.substring(0,100)) do the thing?

like image 702
hsmit Avatar asked Jan 27 '10 14:01

hsmit


People also ask

How are string saved in memory in Java?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

What happens in string memory?

Whenever you create a string object using string literal, that object is stored in the string constant pool and whenever you create a string object using new keyword, such object is stored in the heap memory. For example, when you create string objects like below, they will be stored in the String Constant Pool.

Why are strings stored on the heap?

PermGen space is limited space, the default size is just 64 MB. And it was a problem of creating and storing too many string objects in PermGen space. That's why the String pool is moved to a larger heap area. To make the java more memory efficient the concept of string literal is used.

How much memory do you need to store a string?

An empty String takes 40 bytes—enough memory to fit 20 Java characters.


2 Answers

Doing the following:

data.substring(x, y) + "" 

creates a new (smaller) String object, and throws away the reference to the String created by substring(), thus enabling garbage collection of this.

The important thing to realise is that substring() gives a window onto an existing String - or rather, the character array underlying the original String. Hence it will consume the same memory as the original String. This can be advantageous in some circumstances, but problematic if you want to get a substring and dispose of the original String (as you've found out).

Take a look at the substring() method in the JDK String source for more info.

EDIT: To answer your supplementary question, constructing a new String from the substring will reduce your memory consumption, provided you bin any references to the original String.

NOTE (Jan 2013). The above behaviour has changed in Java 7u6. The flyweight pattern is no longer used and substring() will work as you would expect.

like image 103
Brian Agnew Avatar answered Oct 16 '22 11:10

Brian Agnew


If you look at the source of substring(int, int), you'll see that it returns:

new String(offset + beginIndex, endIndex - beginIndex, value); 

where value is the original char[]. So you get a new String but with the same underlying char[].

When you do, data.substring() + "", you get a new String with a new underlying char[].

Actually, your use case is the only situation where you should use the String(String) constructor:

String tiny = new String(huge.substring(12,18)); 
like image 27
Pascal Thivent Avatar answered Oct 16 '22 10:10

Pascal Thivent