Alex Rivera | Logout

Disposing a StringBuilder object

Asked 2009-08-25T21:08:02.820
32

How does one effectively dispose a StringBuilder object? If an user generates multiple reports in a single sitting, my app ends up using a huge amount of memory.

I've read in a few sites online that the follow may help:

StringBuilder sb = new StringBuilder(1000000);

// loop goes here adding lots of stuff to sb

exampleObject.Text = sb.ToString();

sb.Length = 0;

Does the last line really help? Any other way of dealing with this?

NB: This does not really stop my users from continuing to use the application. I'm just wondering if there is a way of avoiding redundant memory usage.

Edit
Report

1 Answer

2

Unless the 'excessive memory usage' is a problem, I would leave it as it is and not worry about it.

.NET is in most cases smart enough to avoid doing a garbage collection if you have enough memory available.

answered 2009-08-25T21:10:18.870

Your Answer