5

i see this Question

best-way-to-clear-contents-of-nets-stringbuilder/

the answerers set the length to zero and also worries about capacity ? does it really matter to set capacity ?

f we dis-assemble .net 4.5 Library , navigate to System.Text.StringBuilder

       /// <summary>
        /// Removes all characters from the current
 <see cref="T:System.Text.StringBuilder"/> instance.
        /// </summary>
        /// 
        /// <returns>
        /// An object whose <see cref="P:System.Text.StringBuilder.Length"/> 
        ///    is 0 (zero).

  /// </returns>
    [__DynamicallyInvokable]
    public StringBuilder Clear()
    {
      this.Length = 0;
      return this;
    }

is it really matters to set capacity when we already set its length to zero... or ms doesn't care that?

Community
  • 1
  • 1
Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36
  • The most common and usually best way is to simply ditch the StringBuilder and create a new one. Clear() is only useful when you can re-use a really big one ( > 80 kB) . – H H Jan 03 '14 at 09:53
  • That's true for old .NET versions. No longer for 4.0 and up, it now uses *ropes*. – Hans Passant Jan 03 '14 at 11:34

3 Answers3

2

No. It does not matter in every day code. Note that the stack you have linked to specifically addresses ditching that method in favour of the .net 4 implementation.

The Capacity of a StringBuilder dictates the initial size of the underlying buffer. This buffer will expand as needed - I think it simply doubles in capacity each time it needs to grow.

StringBuilder.Length keeps track of how much of the buffer is valid (assigned) data. Capacity and Length are independent.

Capacity is an optimization feature. By zeroing the capacity, all you are doing is ensuring that a memory allocation will have to be performed if you add characters to the StringBuilder in exchange for reclaiming that buffer straight away. It is a classic tradeoff.

Rarely do you need to complicated your code by modifying the capacity.

Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • if Capacity is an optimization feature. , why ms won't worry about... or do they provide other way of doing the same.... – Vishal Sharma Jan 03 '14 at 10:05
  • 1
    @vishalsharma MS do not set it to zero for you because it is often slower - due to more memory allocations. It is available to use because there are some cases where it is required; e.g. low memory devices. If you need to use it, do it manually and make obvious a) that you zeroed the capacity and b) _why_ you did it. – Gusdor Jan 03 '14 at 10:16
2

That depends on the reason why you are clearing the StringBuilder.

If you are reusing a StringBuilder you are likely to create strings that are about the same length as the previous string. Then you would not want to set the Capacity to zero, as you want to keep the current buffer instead of allocating new buffers.

If you have created a very long string, and are about to create substabtially shorter strings with the same StringBuilder, then you want to set the Capacity to zero to get rid of a buffer that is much larger than you need.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

There is one more reason to set the Capacity 0 after a Clear(): The StringBuilder has a memory leak when using Insert() and Clear() repeatedly. Even if there is plenty of free memory available to the Stringbuilder, it will ALWAYS add new memory for an Insert() until an Out of Memory exception is thrown. I reported this bug with a demo program to Microsoft. Please visit the link and upvote the bug so that Microsoft looks into it:

https://connect.microsoft.com/VisualStudio/feedback/details/1242873

For a detailed explanations, see my answer:

Memory Leak in StringBuilder when using Insert() and Clear()

Please note: Setting the Capacity to 0 defeats mostly the purpose of reusing the StringBuilder by using Clear(), because all the memory assigned to the StringBuilder gets lost. I guess using a new StringBuilder is about as fast.

Community
  • 1
  • 1
Peter Huber
  • 3,052
  • 2
  • 30
  • 42