0

I'm appending string variable text, after appending a line throws System.OutOfMemoryException? Can any one explain why it is throwing error.

 str+="something  Text"

 str+="something  Text"

and lastly I assign it to a lable text

When I assign the value of str it throws exception.....

6
  • This single line looks fine. Please provide a complete example. Take your program, remove as much as possible from it, and then post the smallest complete program that reproduces the error you are seeing. Commented Sep 28, 2011 at 8:24
  • sorry i cant post the code.Posting can create issue for me.......... Commented Sep 28, 2011 at 8:26
  • Is there any chance of an infinite loop somewhere? Commented Sep 28, 2011 at 8:28
  • 2
    I think you should use stringbuilder for appending text.... Commented Sep 28, 2011 at 8:31
  • 2
    @Vikky: even if there's no way for you to actually post the code, you should take the time to try to reduce it down to as small as you can get it. If nothing else, it will help YOU decide where the problem lies. And you might end up with a small enough sample that whatever prevents you from posting would allow it. Commented Sep 28, 2011 at 8:32

2 Answers 2

1

I think you have two problems:

  1. You probably shouldn't be using <asp:Label> for the thing you're doing. Try <asp:Literal> instead or provide us with more information on exactly why you're using a label and what it is you want to accomplish (visually, or in HTML terms).
  2. You should build your string with System.Text.StringBuilder and not concatenate it with +=. This will improve performance and reduce memory usage, but since your exception seems to be occurring on label.Text = str;, just replacing the concatenation with StringBuilder won't solve the problem.

Say you have this ASP.NET markup:

<asp:Label id="MyLabel" runat="server" />

and this C# codebehind:

string str = String.Empty;
str += "Some text "
str += "some more text";
MyLabel.Text = str;

Try to replace both of these with:

<asp:Literal id="MyLiteral" runat="server" />

and

StringBuilder sb = new StringBuilder();
sb.Append("Some text ");
sb.Append("some more text");
MyLiteral.Text = sb.ToString();

Another option is to write directly to the output stream with Response.Write(). Without knowing exactly what it is you're trying to accomplish and why you have such a large string that you end up with an OutOfMemoryException, it's hard to help you any further than this, I'm afraid.

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

Comments

0

Probably you're trying to concatenate a lot of strings. Each time you change/concat strings a new string will be produced, because string is an immutable type.

Try using StringBuider to get your big-big string :)

var builder = new StringBuilder();
builder.Append("something  Text");
builder.Append("something  else");
builder.Append("etc.");
builder.ToString();

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.