1

I have looked through all the memory leak solutions for java substring method. I still get the out of memory error due to this issue. I have an arraylist of string which are of length 1000-3500. i index them and store them. The issue is each string needs to be run through loop to store all possible varying lengths of same string. To do this, i use for loop and substring method. and this method causes the memory leak problem.

A sudo code of what i have done:

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

str: string. and this above loops runs till all the string within the arraylist are indexed. I tried to fix the leak by,

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 the issue. My java version is: 1.7.0_17.

Edit:

I understand this is not a memory leak problem from the comments. I am indexing some continuous strings. Say for example,

String s= abcdefghijkl;

i want index each string as :

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

To perform this,i get a string,then perform substring operation,get that string and index them.

3
  • 1
    How did you find out there is a leak? Commented Jun 28, 2013 at 7:08
  • 2
    Yes, are you sure it's a leak rather than just a large usage of memory? Have you tried increasing your heap size? Please also post an SSCCE that demonstrates the leak. Commented Jun 28, 2013 at 7:09
  • 2
    Why are you doing that in the first place? This looks like an XY problem. Care to tell what you want to achieve exactly? Commented Jun 28, 2013 at 7:14

2 Answers 2

4

There is no leak.

Please note that you're creating a huge amount of String objects. If a String has a length of 1000 characters you're creating 1000 objects.

Is it really needed to create so many String objects? Would it be possible for example to use a char[] to achive what you want?

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

Comments

1

There are 2 things:

First: ".intern()" keeps the string in an internal cache that is usually not garbage collected - please don't use it if you're not 100% sure why you are using it.

Second: there is a constructor from String taking char[] like this:

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);
}

-> this is also more efficient (in terms of speed)

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.