473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

StringBuilder OutOfMemory

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

--

Jun 14 '06 #1
7 24407
Hi Arnd,

can you show me some code where the exception occurs? Maybe, I can be
of help.

Cheers.


--


Jun 14 '06 #2
> 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


StringBuilder doesn't reserve MaxCapacity in memory, but a lower value
that you can specify when you instantiate it (or accept the default
value of 16 chars).
When you want to add text, so that the current capacity is not enough
anymore, then StringBuilder will try to *double* it's capacity.

So in your case it will want to go from 17MB to 34MB. I don't know if
it needs a *continuous* block of memory.

try it with a
StringBuilder sb = new StringBuilder(<your target capacity>);
Hans Kesting
Jun 14 '06 #3

"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
|
| --
|

You can't create a StringBuilder with "MaxCapacity is 2.147.483.647". The
largest StringBuilder on the current CLR versions available is ~1G
characters, but even such a SB can't be created on 32 bit Windows, you need
to run 64 bit windows for this. On 32 bit Windows the largest SB you are
able to create is subject of the amount of free "contiguous" virtual address
space, the size of free space highly depends on the amount of fragmentation
of both the 'process heap' and the 'large object heap', but it general it's
much smaller than ~1.2 GB - 1.6Gb (or ~600K - 800K characters). If your heap
is highly fragmented (which I guess it's the case here), it's even possible
that you can't allocate a 17.825.779 characters SB.

Willy.

Jun 14 '06 #4
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

--

Jun 14 '06 #5
hi
post the code of the method
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"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

--

Jun 14 '06 #6
Willy Denoyette [MVP] wrote:

"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

--


You can't create a StringBuilder with "MaxCapacity is 2.147.483.647".
The largest StringBuilder on the current CLR versions available is
~1G characters, but even such a SB can't be created on 32 bit
Windows, you need to run 64 bit windows for this. On 32 bit Windows
the largest SB you are able to create is subject of the amount of
free "contiguous" virtual address space, the size of free space
highly depends on the amount of fragmentation of both the 'process
heap' and the 'large object heap', but it general it's much smaller
than ~1.2 GB - 1.6Gb (or ~600K - 800K characters). If your heap is
highly fragmented (which I guess it's the case here), it's even
possible that you can't allocate a 17.825.779 characters SB.

Willy.


Thanks for the explanation!

--

Jun 14 '06 #7
Try this:

string s = new string('a', 1024*1024*32);
StringBuilder sb = new StringBuilder(1024*512); // start with a SB of
512K chars.
for (int i = 0; i < 100; i++)
{
Console.WriteLine(sb.Capacity);
sb.Append(s);
}
The output should look like:
524288
33554432
67108864
134217728
134217728
268435456
268435456
268435456
268435456

notice the SB expanding, but as the SB expands, the preceeding buffer cannot
be re-used as it's contents must be preserved in order to copy the contents
to the new buffer. When the SB has expanded a second (and third and
fourth...) time, the preceeding "free buffers" are simply too small to hold
the new required SB buffer (they can be used to hold other objects however).
This results in a max. SB of 268435456 chars. in this sample.

That is why you should pre-allocate your SB when you have to deal with very
large strings. To prove this, just change the initial size into 620Mb
(1024*1024*620) and all should be fine for this simple console application,
but no-one will guarantee that it will do for a more complex program, nor
will it work for a Windows Forms application.
Willy.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uw**************@TK2MSFTNGP03.phx.gbl...
| hi
|
|
| post the code of the method
|
|
| --
| --
| Ignacio Machin,
| ignacio.machin AT dot.state.fl.us
| Florida Department Of Transportation
|
| "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
| >
| > --
| >
|
|
Jun 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ccwork | last post by:
Hi, I get an interesting question. I program an MDI that each JInternalFrame will show an image of 1700x1500. I limit that 2 JInteralFrames (and thus 2 images) can be opened at most. When users...
3
by: Mike Schilling | last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy test to verify this: class OOM { public static void Main() { try { Object arr = new Object; } catch...
2
by: Ricky Chan | last post by:
Windows Server 2003 with 2G Ram ,IIS6 -> I have enabled /3gb switch in boot.ini ! -> Machine.config set to 80% memory Limit However, It still throw outofmemory exception when loading large...
1
by: Ricky Chan | last post by:
In the production environment, it always occurs and the worker process did not recycle automatically. Therefore, it make the system service break to client. In development environment, we write...
6
by: Lenny Wintfeld | last post by:
Hi I'm attempting additions/changes to a Java program that (among other things) uses XSLT to transform a large (96 Mb) XML file. It runs fine on small XML files but generates OutOfMemory...
1
by: Arnd Iffland | last post by:
Hi, i use a StringBuilder for a large string. After instantiation, MaxCapacity is 2.147.483.647 which is fairly large enough for my purpose. During my procedure, i get a...
2
by: Peter S. | last post by:
I am pulling some data from a source via ODBC and placing the information in a DataSet. The first pull is very large but once that is complete I plan to do nightly pulls to get any new data that...
0
by: Neil Stephens | last post by:
We have an ASP.NET (VB) process that is creating a stringbuilder object which is failing under a particular set of circumstances. It runs fine on a few dozen servers except for one where an OOM...
2
by: cS944 | last post by:
I have a program that reads AD and writes a table. I was using stringbuilder and completely re-wrote the app using OleDb & Parameters. Eitherway (with StringBuilder or without), when it runs in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.