1

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?

Kazoom
  • 5,659
  • 16
  • 56
  • 69
  • 2
    Please explain why you need to keep a 100MB string in memory - perhaps there is a better solution. – Andrew Hare Apr 19 '10 at 22:00
  • 1
    thats how the code has always been, its kind of an old code and now some requirements has been changed – Kazoom Apr 19 '10 at 22:21

6 Answers6

1

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.

Community
  • 1
  • 1
Josh
  • 68,005
  • 14
  • 144
  • 156
1

By going so large your strings are moved to the Large Object Heap (LOH) and you run a greater risk of fragmentation.

A few options:

  1. Use a StringBuilder. You will be re-allocating less frequently. And try to pre-allocate, like new StringBuilder(100*1000*1000);

  2. re-design your solution. There must be alternatives to keeping such large strings around. A List<string> for instance, that is only converted to 1 single string when (really) necessary.

H H
  • 263,252
  • 30
  • 330
  • 514
1

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
  • 2,403
  • 1
  • 18
  • 23
0

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
  • 102,129
  • 8
  • 104
  • 120
0

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.

0

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
  • 63,072
  • 85
  • 231
  • 359