XYZ
XYZ

Reputation: 119

String Builder and string size

Why StringBuilder Size is greater than string(~250MB).

Please read the question. I want to know the reason of size constraint in the string, but not in stringbuilder. I have fixed the problem of reading file.

Yes, I know there are operation, we can perform on string builder like append, replace, remove, etc. But what is the use of it when we can't get ToString() from it and we can't write it directly in the file. We had to get ToString() to actually use it, but because its size is out of string range it throws exception.

So in particular is there any use of string builder having size greated than string as i read a file of around 1 gb into string builder but cant get it into string. I read all the pros and cons of StringBuilder over String but I cant anything explaning this

Update: I want to load XMLDocument from file if reading in chunk then data cannot be loaded because root level node needs its closing tag which will be in other chunk block

Update: I know it is not a correct aproach now i am different process but still i want to know the reason of size constraing in string but not in stringbuilder

Update: I have Fixed my proble and want to know the reason why there is no memory constraint on stringbuilder.

Upvotes: 1

Views: 4006

Answers (3)

weston
weston

Reputation: 54781

Why StringBuilder Size is greater than string(~250MB).

The reason depends on the version of .net.

There are two implementations Eric Lippert mentions here: https://stackoverflow.com/a/6524401/360211

Internally a string builder maintains a char[]. When you append it may have to resize this array. In order to stop it needing to be resized every time you append it resizes to a larger size to anticipate future appends (it actually doubles in size). So the StringBuilder often ends up larger than it's content, as much as double the size.

A newer implementation maintains a linked list of char[]. If you do many small appends, the overhead of the linked list may account for the extra 250MB.

In normal use, an extra 100% size on a string temporarily doesn't make one bit of difference given the performance benefits, but when you are dealing with a GB, it becomes significant and that is not its intended usage.

Why you get OutOfMemoryException

The linked list implementation can fit more in memory than a string because it does not need one continuous block of 1GB. When you ToString it would force it to try to find another GB, which is also continuous and that is the problem.

Why is there no constraint preventing this?

Well there is. The constraint is if there is not enough memory to create a string during ToString, throw an OutOfMemoryException.

You may want this to happen during Append operations, but that would be impossible to determine. StringBuilder could look at the free memory, but that might change before you call ToString. So the author of StringBuilder could have set an arbitrary limit, but that can't suit all systems equally, as some will have more memory than others.

You also might want to do operations that reduce the size of the StringBuilder before calling ToString, or not call ToString at all! So just because StringBuilder is too large to ToString at any point is not a reason to throw an exception.

Upvotes: 6

abhinav pandey
abhinav pandey

Reputation: 584

You can try the following to handle large XML files. CodeProject

Upvotes: 0

Heinzi
Heinzi

Reputation: 172200

You can use StringBuilder.ToString(int, int) to get smaller-sized chunks of your huge content out of of the StringBuilder.

In addition, you might want to consider whether you are really using the right tool for the job. StringBuilder's purpose is to build and modify strings, not to load huge files to memory.

Upvotes: 5

Related Questions