2012-05-02 45 views
1

我發現下面的代碼在我老闆的項目:創建一個StringBuilder具有大容量

Dim strBuilder As New System.Text.StringBuilder("", 1000000) 

之前我叫他出來就可以了,我想確認該行是否實際上是設置一兆字節(或Unicode中的兩兆字節)內存,而不是那一個stringbuilder?

+0

我不認爲有過一個理由這樣做。 – Jason

+0

同意。在面對我的老闆之前,我只需要一些確定的立場。 :) – MCattle

回答

3

初始化長度1000000

Char()所以在存儲器所需的實際尺寸是2000000字節 =〜2 MB由於炭是unicode,需要2個字節。

編輯:萬一你的老闆不相信,這反映了ILSpy:

// System.Text.StringBuilder 
[SecuritySafeCritical] 
public unsafe StringBuilder(string value, int startIndex, int length, int capacity) 
{ 
    if (capacity < 0) 
    { 
     throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", new object[] 
     { 
      "capacity" 
     })); 
    } 
    if (length < 0) 
    { 
     throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", new object[] 
     { 
      "length" 
     })); 
    } 
    if (startIndex < 0) 
    { 
     throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex")); 
    } 
    if (value == null) 
    { 
     value = string.Empty; 
    } 
    if (startIndex > value.Length - length) 
    { 
     throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_IndexLength")); 
    } 
    this.m_MaxCapacity = 2147483647; 
    if (capacity == 0) 
    { 
     capacity = 16; 
    } 
    if (capacity < length) 
    { 
     capacity = length; 
    } 
    this.m_ChunkChars = new char[capacity]; 
    this.m_ChunkLength = length; 
    fixed (char* ptr = value) 
    { 
     StringBuilder.ThreadSafeCopy(ptr + (IntPtr)startIndex, this.m_ChunkChars, 0, length); 
    } 
} 
1

您可以嘗試在撥款前後撥打GC.GetTotalMemory(),查看是否增加。注意:這不是一個好的,科學的方式來做到這一點,但可能證明你的觀點。