tags:

views:

133

answers:

5

By what i understand String and StringBuilder objects both allocate contiguous memory underneath.

My program runs for days buffering several output in a String object. This sometimes cause outofmemoryexception which i think is because of non availability of contiguous memory. my string size can go upto 100MBs and i m concatenating new string frequently this causes new string object being allocated. i can reduce new string object creation by using Stringbuilder but that would not solve my problem entirely

Is there an alternative to a contiguous string object?

A: 

I don't believe there's any solution for this using either String or StringBuilder. Both will require contiguous memory. Is it possible to change your architecture such that you can save the ongoing data to a List, a file, a database, or some other structure designed for such purposes?

Ben Hoffstein
+1  A: 

A rope data structure may be an option but I don't know of any ready-to-use implementations for .NET.

Have you tried using a LinkedList of strings instead? Or perhaps you can modify your architecture to read and write a file on disk instead of keeping everything in memory.

Josh Einstein
A: 

First you should examine why you are doing that and see if there are other things you can do that give you the same value.

Then you have lots of options (depending on what you need) ranging from using logging to writing a simple class that collects strings into a List.

+1  A: 

DO NOT USE STRINGS.

Strings will copy and allocate a new string for every operation. That is, if you have an 50mb string and add one character, until garbage collection happens, you will have two (aprox) 50mb strings around. Then, you add another char, you'll have 3.... and so on.

On the other hand, proper use of StringBuilder, that is, using "Append" should not have any problem with 100 mbs.

Another optimization is creating the StringBuilder with your estimated size,

StringBuilder SB;

SB= new StringBuilder(capacity); // being capacity the suggested starting size

Use stringBuider to hold your big string, and then use append.

HTH

Daniel Dolz
A: 

You can try saving the string to a database such as TextFile, SQL Server Express, MySQL, MS Access, ..etc. This way if your server gets shutdown for any reason (Power outage, someone bumped the UPS, thunderstorm, etc) you would not lose your data. It is a little slower then RAM but I think the trade off is worth it.

If this is not an option -- Most definitly use the stringbuilder for adding strings.

Luke101