Hi Arnd,
It's hard to say for sure what caused your OOM exception. There are many
possible factors involved. In fact, there are no methods of StringBuilder
that are documented to throw an OOM exception. So, it is entirely possible
that the StringBuilder did not throw the exception, but that the exception
was thrown as a result of a StringBuilder operation that resulted in a
memory allocation exceeding the amount of available memory on the system.
What I mean is, it looks to me like the problem is not related to the
MaxCapacity or Capacity properties of the StringBuilder, asevidenced by your
statistics. However, using a StringBuilder can cause memory allocation to
occur at different points, and this memory allocation may have caused the
exception.
For example, the default Capacity of a StringBuilder is 16 characters. When
you append more than 16 characters, the Capacity is doubled, and is doubled
again each time the length of the string exceeds the Capacity. Each time
this occurs, more memory must be allocated. Since it is allocated piecemeal,
you may be experiencing the result of memory fragmentation. This can be
avoided by setting the Capacity to a high number before beginning the
building of the String.
Another possibility lies in the usage of the ToString method of the
StringBuilder. The first time this is called, a reference to the actual
string in the buffer is returned. If you append to the String afterwards,
the existing string is copied to the buffer, so that the string reference
returned by the previous ToString call is not altered by your future
StringBuilder modification code. This means that additional memory is
allocated to hold the existing string buffer, and the new buffer.
You can read more about the technical details here:
http://www.codeproject.com/dotnet/strings.asp
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
A lifetime is made up of
Lots of short moments.
"Arnd Iffland" <if*****@rdv.de> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,
i want to use a StringBuilder to create a large string.
After instatiation, the MaxCapacity is 2.147.483.647, what is large
enough for my purpose.
During my procedure i get a SystemOutOfMEmoryException at
System.String.GetStringForBuilder
The length of the StringBuilder is at this moment 17.825.779 and the
capacity 17.825.792.
Why do i get this exception? What can i do to get the MAxCapacity?
There is no other StringBuilder in the procedure.
The procedure is a WebService build with WebDeveloper Express and .Net
2.0.50727.42
Best regards,
Arnd
--