Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substring method in String class causes memory leak [duplicate]

It is said that substring method in String class causes memory leak. Is it true? How? What is an alternative for it?
Especially looking for answer,
What are all other things which can causes of memory leak in java? This will help me to take care while coding.

like image 257
AmitG Avatar asked Mar 25 '13 10:03

AmitG


People also ask

What is the memory leak issue in string class?

A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory, and therefore, they're unnecessarily maintained. A memory leak is bad because it blocks memory resources and degrades system performance over time.

What happens in string pool when we use substring method?

Answer: As we know, the String class is Immutable and substring() method is an inbuilt method of the String class. Every time you perform an operation on the String, the subsequent String is a new String that is returned. The same thing happens with this method as well.

What are the causes of memory leaks?

Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.

Which action causes memory leak?

In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.


2 Answers

In past versions of the JDK, the implementation of the substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. You could thus inadvertently keep a reference to a very big character array with just a one character string. Here's an example of a bug this could induce.

This method has now been changed and this "leak" doesn't exist anymore.

If you want to use an old JDK (that is older than OpenJDK 7, Update 6) and you want to have minimal strings after substring, use the constructor taking another string :

String s2 = new String(s1.substring(0,1));

As for your second question, regarding " other things which can causes of memory leak in java", it's impossible to answer in a constructive way. There aren't in java standard libs many instances of cases where you could so easily keep hidden references to objects. In the general case, pay attention to all the references you build, the most frequent problems probably arising in uncleaned collections or external resources (files, database transactions, native widgets, etc.).

like image 84
Denys Séguret Avatar answered Oct 10 '22 23:10

Denys Séguret


The substring() method doesn't allocate a new character array for a String, but rather simply produces a String with a window onto the existing char array. This is an impementation of a flyweight pattern and was regarded as an optimisation.

So if I have a huge String (char array) and then create a substring, even if I garbage collect the original string, the original char array remains (despite the fact that you think you have a substring of, say, 2 chars). This problem is often encountered when (say) parsing a huge stream of input data (perhaps an XML file) and extracting a small amount of text via substring()

Using the seemingly redundant String(String str) constructor (a String constructor taking a String!) resolves this since it allocates a new (potentially smaller) char array, allowing the original to be garbage collected.

Note this behaviour has changed as of Java 7u6.

like image 28
Brian Agnew Avatar answered Oct 11 '22 00:10

Brian Agnew