is Capacity property of Stringbuilder really matters when we set its length to zero?

StackOverflow https://stackoverflow.com/questions/20900335

  •  23-09-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top