Memory leak issue when using java substring method

I looked through all the memory leak solutions for the java substring method. Due to this problem, I still get an error from memory. I have an arraist from a string 1000-3500 long. I index them and store them. The problem is that each line must run through a loop to store all the possible variables of the length of the same line. For this, I use the loop and substring method. and this method causes a memory leak problem.

The sudo code of what I did:

for(int i=0;i<str.length;i++)
{
    //create substring and index it
    str.substring(0,(str.length()-i));
}

str: string. and these loops above are executed until the entire line inside the arraylist is indexed. I tried to fix the leak,

1.

for(int i=0;i<str.length;i++)
{
    //create substring and index it
    new String(str.substring(0,(str.length()-i)));
}

2.

for(int i=0;i<str.length;i++)
{
    //create substring and index it
    new String(str.substring(0,(str.length()-i)).intern());
}

3.

for(int i=0;i<str.length;i++)
{
    //create substring and index it
    new String(str.substring(0,(str.length()-i))).intern();
}

Still, I have a problem. My java version: 1.7.0_17.

Edit:

, . . , ,

String s= abcdefghijkl;

:

abcdefghjk
abcdefghj
abcdefhg
abcdefh
abcdef
abcde
abcd
..
..
a

, , , .

+1
3

.

, String. a String 1000 , 1000 .

String? , , char[] , ?

+4

2 :

-: ".intern()" , , , 100%, .

-: String, char [] :

final char[] chars = str.toCharArray ();
for(int i=0;i<chars.length;i++)
{
    //create substring and index it
    new String(chars, 0, chars.length-i);
}

- > ( )

+1

This issue has been fixed in JDK 1.7 by returning a new copy of the character array.

public String(char value[], int offset, int count) {
//check boundary
 // It return new copy on array.
this.value = Arrays.copyOfRange(value, offset, offset + count);
}

public String substring(int beginIndex, int endIndex) {
//check boundary
int subLen = endIndex - beginIndex;
return new String(value, beginIndex, subLen);
}

http://javaexplorer03.blogspot.in/2015/10/how-substring-memory-leak-fixed-in-jdk.html?q=itself+implements+Runnable

+1
source

Source: https://habr.com/ru/post/1570140/


All Articles