Re: StringBuilder vs. String performance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Kevin Spencer <kevin@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I should have said "the most important point." I was quoting the previous
message. The predominant problems that occur with strings are centered
around memory accumulation. Speed is certainly a consideration, but not
likely to bring the app to a crashing halt, and not as much of an issue
unless there is a heck of a lot of string-building going on, in which case
memory is more likely to cause real problems.

Using string concatenation badly will cause a lot of GC work, but it
shouldn't actually bring the app to a crashing halt. With the typical
"append in a loop" which is what StringBuilder is usually the cure for,
each string is eligible for GC after a single extra iteration of the
loop, so although lots of gen0 strings are created, it shouldn't be an
issue. If the strings go into the large object heap, that's a different
matter - but then StringBuilder will have a similar problem, just less
pronounced.

Pretty much every question I've answered with a reply of "use
StringBuilder instead" has been due to someone complaining about speed,
not memory.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: .NET SUCKS --- READ FOLLOWING. MICROSOFT IS A SUCKY CO
    ... > system just doesn't cut it in high memory load situations. ... Garbage Collection is not an excuse for poor memory management. ... One excellent example is the use of strings. ... because many "professional" programmers know very little about what is ...
    (microsoft.public.dotnet.framework)
  • Re: Secure Credentials pwd handling
    ... strings that are produced when you read the properties. ... Co-author of "The .NET Developer's Guide to Directory Services Programming" ... so at some point the value will be in memory. ... SecureString is added to .NET to support this use case. ...
    (microsoft.public.dotnet.security)
  • Re: CStrings and memory
    ... Then you allocate a few more strings ... Now you need to allocate another string, but there is no more memory. ... In addition to strings, there are other things that get allocated: ... have perhaps confused "address space" with "working set". ...
    (microsoft.public.vc.mfc)
  • Re: Verbose functional languages?
    ... whereas a memory leak due to too much laziness in the wrong place can be. ... If you declare your data structure elements as strict, ... I think size matters, for the optimization heuristics. ... strings for symbol names. ...
    (comp.lang.functional)
  • Re: mex: cell (of strings) allocation
    ... strings and numeric data, and returns the numeric data in a ... and the strings in a cell array of strings. ... cannot seem to use the same idiom for the cell of strings. ... *exactly* the same amount of memory you originally ...
    (comp.soft-sys.matlab)