1

I need to concatinate few strings. I am using StringBuilder for that as below

        StringBuilder result = new StringBuilder(length);

The length is the "length" of the string that i want at the end.

"Length" is of higher number. To get string i need to do

        return result.ToString();

When i am trying to analyze memory consumed by my application i see that "StringBuilder" and "String" are taking same amount of memory and its duplicate. As the length of the string is long, its occupying larger % of memory.

Is there a better way to solve this memory problem?

4
  • It's hard to answer the question without knowing the problem you're trying to solve. But if your application uses too much memory because of large strings, then it looks like you shouldn't be using strings at all. A streaming approach might be better then.
    – Dirk
    Commented Apr 2, 2015 at 8:18
  • Yeah, we need to serialize it and need to read from other side. So we are using String. Commented Apr 2, 2015 at 8:20
  • 1
    Well, if you want to use strings then you have to live with the consequences. But if you use the StringBuilder to concatenate a known (at compile time) number of strings then it might be better to use the +-operator, since that will be translated into a String.Concat by the compiler and might avoid allocating memory for a temporary object.
    – Dirk
    Commented Apr 2, 2015 at 8:29
  • 1
    solve this memory problem? Is it a problem? Or do you just have so much free memory the garbage collector isn't running? Commented Apr 2, 2015 at 8:41

1 Answer 1

1

To solve what memory problem? You've observed a behavior, but not explained in what way it's a problem.

Note that immediately after the statement return result.ToString(); executes, the StringBuilder object referenced by result is eligible for garbage collection (assuming that's the only reference to the object). So any theoretical problem should be transient, with the likely outcome of little or no actual impact.

Put another way: suppose you build 100 strings of length N. These strings have a nominal overhead of two bytes per character, so the in-memory cost is 200 * N. During the whole time you are building those strings, the nominal in-memory cost of the extra StringBuilder object is N. Yes, it's possibly more than one of those objects could exist at a time, but only when it doesn't matter. Otherwise, .NET would garbage collect the old one to make room for the new one.

So your end result is an overhead of 1% for that extra StringBuilder, a lot less than the doubling of memory your question implies. And that's if you are building only 100 strings. The effective overhead is inversely proportional to the number of strings you actually create, and of course the importance of the overhead is directly proportional to that same number. In other words, the more it matters, the less the actual impact there is.


More generally, what kind of option do you think you would find? The StringBuilder class is the best way to deal with a mutable string, i.e. to provide a way to create a string from parts or to otherwise edit a string (e.g. delete pieces from it, rearrange it, etc.). That said, you could implement your own string-editing class to accomplish a similar effect in a specialized way.

But regardless of what you are using to edit the string, if when you are done you want to have an instance of System.String, you are necessarily going to have two copies of the data in the string: one in the editable version, and one in the final System.String object. You can't edit a System.String object (the type is immutable), and you can't magically in-place turn some other type into an instance of System.String.


(Though as an aside, I will note that in an older implementation of StringBuilder, the buffer from the StringBuilder was in fact just copied as a reference over to the new System.String object. No new copy of the string data was created unless at some point after calling ToString(), the StringBuilder object was modified again. The current implementation of StringBuilder doesn't do this though. It's optimized for scenarios involving longer strings, avoiding the "double the buffer size" reallocation cost at the expense of always requiring a copy of the data for that final ToString() call).


Now, if you can tolerate using the string data as the original editing object (i.e. StringBuilder or some other custom class) and never converting the data to an instance of System.String, then that's obviously a way to avoid the second copy of data. In that approach, you just never bother with the ToString() call. Ever.

But otherwise, you seem to be asking for the impossible, as there is no mechanism available for taking an existing buffer of characters and forcing System.String to use that buffer as its internal representation. I.e. it is inherent in the process of converting any other data structure (including StringBuilder) to an instance of System.String, that a second copy of that data would be made.

1
  • Thanks for such a nice explanation. I hope i need to change the Datatype from String to bytes or some other datatypes. Because string is just being used for communication from one system to another system. Commented Apr 7, 2015 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.