1

i'm having a string(MyString) of length 9700000(approximately). and a for loop to get substring from this String

like

for(int i=0;i<MyString.Length;i+=1000)
  {
    String SStr=MyString.Substring(i,1000);
  }

in this loop i'm geting an exception like OutOfMemory Exception. But if i assign the MyString to a temporary string and using it(temporary string) instead of MyString in the for loop,then its working fine. What will be the reason for this. And how its stored in memory?

EDIT : I used StringBuilder too

Like

for(int i=0;i<MyStringBuilder.Length;i+=1000)
   {
    String SStr=MyStringBuilder.ToString().Substring(i,1000);
   }

But the same thing is happening.

Edit MyString and MyStringBuilder are Global Variables

5
  • Try using StringBuilder instead Commented Nov 24, 2010 at 10:18
  • 5
    I wish you'd post the actual code you're using, rather than keep editing the for loop to various different flavours of broken. Commented Nov 24, 2010 at 10:23
  • What is 'MyString'? If it's a local variable or a field, change its name not to start with a capital letter. If it's a property getter, then you need to post the code for that, because (in the latest version of the question), you're now calling it about 20000 times, so what it does might be significant Commented Nov 24, 2010 at 10:27
  • You need to post more codes so people can reproduce the same error. Commented Nov 24, 2010 at 10:51
  • @ All : Sorry for the continuous change in my question Commented Nov 24, 2010 at 10:54

2 Answers 2

3

It looks like MyString might be a property which is itself creating a new string on each call.

Your 800 SubString calls will use about 2MB of memory, which is unlikely to be the problem, but if your 800 calls to MyString each allocate a new copy of your 9.7MB string, then that will be an issue.

Edit:

Answering this question is a moving target, because you keep changing it, but your new StringBuilder example calls ToString for each time around the loop, which is going to make another copy of your original string - that's going to need about 9700000 * 9700000/1000 characters of space, which although the garbage collector may come by and save you, is an awful lot of allocation work.

Your life, and everyone else's life, would be improved if you avoided using the names of classes (e.g. 'StringBuilder') as the names of variables. Use a different naming convention (for example, start with a lower-case letter on variables)

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

Comments

3

I'm not sure this loop will ever terminate.

Shouldn't it read:

for(int i=0;i<800; i+= 1000 )
{
    String SStr=MyString.Substring(i,1000);
}

So that i is increasing.

You will also want to change your upper bound (i<800)

Or perhaps what you're actually trying to do is:

for(int i=0;i<800; i++)
{
    String SStr=MyString.Substring( i * 1000 ,1000);
}

1 Comment

Yeah, just edited my answer to suggest a different way of running the loop.

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.