首页 > 代码库 > List的Capacity

List的Capacity

  Capacity  

在.NET中List的容量应该只是受到硬件限制。

属性Capacity的真正含义,是创建List时给它预分配的容量。

一旦项的数量超过了当前的Capacity,Capacity会以

Capacity = (Capacity==0)?A_VALUE_YOU_CANNOT_SURE:(Capacity*2)

的方式增长。

当使用默认构造函数创建一个List时,Capacity就是0。(或者你非要用一个0作为capacity参数调用构造)

所以当Capacity是0时,我们为List添加了第一个项目后,Capacity是多少呢?

对,是A_VALUE_YOU_CANNOT_SURE

不过,在.NET2.0和.NET4.5中,这个值看上去都是4。

            System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity));            int initCapacity = 0;            List<string> l = new List<string>(initCapacity);            System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity));            for (int u = 0; u < 65; ++u)            {                System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity));                //l.TrimExcess();                //System.Diagnostics.Debug.WriteLine(string.Format("    TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity));                l.Add("1");            }

输出

default capacity is : 0constructe a list with capacity: 0Count: 0, Capacity: 0Count: 1, Capacity: 4Count: 2, Capacity: 4Count: 3, Capacity: 4Count: 4, Capacity: 4Count: 5, Capacity: 8Count: 6, Capacity: 8Count: 7, Capacity: 8Count: 8, Capacity: 8Count: 9, Capacity: 16Count: 10, Capacity: 16Count: 11, Capacity: 16Count: 12, Capacity: 16Count: 13, Capacity: 16Count: 14, Capacity: 16Count: 15, Capacity: 16Count: 16, Capacity: 16Count: 17, Capacity: 32Count: 18, Capacity: 32Count: 19, Capacity: 32Count: 20, Capacity: 32Count: 21, Capacity: 32Count: 22, Capacity: 32Count: 23, Capacity: 32Count: 24, Capacity: 32Count: 25, Capacity: 32Count: 26, Capacity: 32Count: 27, Capacity: 32Count: 28, Capacity: 32Count: 29, Capacity: 32Count: 30, Capacity: 32Count: 31, Capacity: 32Count: 32, Capacity: 32Count: 33, Capacity: 64Count: 34, Capacity: 64Count: 35, Capacity: 64Count: 36, Capacity: 64Count: 37, Capacity: 64Count: 38, Capacity: 64Count: 39, Capacity: 64Count: 40, Capacity: 64Count: 41, Capacity: 64Count: 42, Capacity: 64Count: 43, Capacity: 64Count: 44, Capacity: 64Count: 45, Capacity: 64Count: 46, Capacity: 64Count: 47, Capacity: 64Count: 48, Capacity: 64Count: 49, Capacity: 64Count: 50, Capacity: 64Count: 51, Capacity: 64Count: 52, Capacity: 64Count: 53, Capacity: 64Count: 54, Capacity: 64Count: 55, Capacity: 64Count: 56, Capacity: 64Count: 57, Capacity: 64Count: 58, Capacity: 64Count: 59, Capacity: 64Count: 60, Capacity: 64Count: 61, Capacity: 64Count: 62, Capacity: 64Count: 63, Capacity: 64Count: 64, Capacity: 64
View Code


  TrimExcess()   

TrimExcess()的意思就是,如果Capacity大于Count,清掉哪些没有使用的空间。基本上可以理解成让Capacity等于Count,只是基本上,不能信赖这一点。.NET毕竟还要考虑很多性能问题。TrimExcess method does nothing if the list is at more than 90 percent of capacity——这就是.NET的方式。实际上把上面那段代码中的注释放开,就会看到确实存在Capacity和Count不等的情况(然而,并不是90%哟)。Capacity不够以后的增长规则,仍然是*2,并不会因为被Trim过就只是加1。

放开注释后的输出

default capacity is : 0constructe a list with capacity: 0Count: 0, Capacity: 0    TrimExcess() -> Count: 0, Capacity: 0Count: 1, Capacity: 4    TrimExcess() -> Count: 1, Capacity: 1Count: 2, Capacity: 2    TrimExcess() -> Count: 2, Capacity: 2Count: 3, Capacity: 4    TrimExcess() -> Count: 3, Capacity: 4Count: 4, Capacity: 4    TrimExcess() -> Count: 4, Capacity: 4Count: 5, Capacity: 8    TrimExcess() -> Count: 5, Capacity: 5Count: 6, Capacity: 10    TrimExcess() -> Count: 6, Capacity: 6Count: 7, Capacity: 12    TrimExcess() -> Count: 7, Capacity: 7Count: 8, Capacity: 14    TrimExcess() -> Count: 8, Capacity: 8Count: 9, Capacity: 16    TrimExcess() -> Count: 9, Capacity: 9Count: 10, Capacity: 18    TrimExcess() -> Count: 10, Capacity: 10Count: 11, Capacity: 20    TrimExcess() -> Count: 11, Capacity: 11Count: 12, Capacity: 22    TrimExcess() -> Count: 12, Capacity: 12Count: 13, Capacity: 24    TrimExcess() -> Count: 13, Capacity: 13Count: 14, Capacity: 26    TrimExcess() -> Count: 14, Capacity: 14Count: 15, Capacity: 28    TrimExcess() -> Count: 15, Capacity: 15Count: 16, Capacity: 30    TrimExcess() -> Count: 16, Capacity: 16Count: 17, Capacity: 32    TrimExcess() -> Count: 17, Capacity: 17Count: 18, Capacity: 34    TrimExcess() -> Count: 18, Capacity: 18Count: 19, Capacity: 36    TrimExcess() -> Count: 19, Capacity: 19Count: 20, Capacity: 38    TrimExcess() -> Count: 20, Capacity: 20Count: 21, Capacity: 40    TrimExcess() -> Count: 21, Capacity: 21Count: 22, Capacity: 42    TrimExcess() -> Count: 22, Capacity: 22Count: 23, Capacity: 44    TrimExcess() -> Count: 23, Capacity: 23Count: 24, Capacity: 46    TrimExcess() -> Count: 24, Capacity: 24Count: 25, Capacity: 48    TrimExcess() -> Count: 25, Capacity: 25Count: 26, Capacity: 50    TrimExcess() -> Count: 26, Capacity: 26Count: 27, Capacity: 52    TrimExcess() -> Count: 27, Capacity: 27Count: 28, Capacity: 54    TrimExcess() -> Count: 28, Capacity: 28Count: 29, Capacity: 56    TrimExcess() -> Count: 29, Capacity: 29Count: 30, Capacity: 58    TrimExcess() -> Count: 30, Capacity: 30Count: 31, Capacity: 60    TrimExcess() -> Count: 31, Capacity: 31Count: 32, Capacity: 62    TrimExcess() -> Count: 32, Capacity: 32Count: 33, Capacity: 64    TrimExcess() -> Count: 33, Capacity: 33Count: 34, Capacity: 66    TrimExcess() -> Count: 34, Capacity: 34Count: 35, Capacity: 68    TrimExcess() -> Count: 35, Capacity: 35Count: 36, Capacity: 70    TrimExcess() -> Count: 36, Capacity: 36Count: 37, Capacity: 72    TrimExcess() -> Count: 37, Capacity: 37Count: 38, Capacity: 74    TrimExcess() -> Count: 38, Capacity: 38Count: 39, Capacity: 76    TrimExcess() -> Count: 39, Capacity: 39Count: 40, Capacity: 78    TrimExcess() -> Count: 40, Capacity: 40Count: 41, Capacity: 80    TrimExcess() -> Count: 41, Capacity: 41Count: 42, Capacity: 82    TrimExcess() -> Count: 42, Capacity: 42Count: 43, Capacity: 84    TrimExcess() -> Count: 43, Capacity: 43Count: 44, Capacity: 86    TrimExcess() -> Count: 44, Capacity: 44Count: 45, Capacity: 88    TrimExcess() -> Count: 45, Capacity: 45Count: 46, Capacity: 90    TrimExcess() -> Count: 46, Capacity: 46Count: 47, Capacity: 92    TrimExcess() -> Count: 47, Capacity: 47Count: 48, Capacity: 94    TrimExcess() -> Count: 48, Capacity: 48Count: 49, Capacity: 96    TrimExcess() -> Count: 49, Capacity: 49Count: 50, Capacity: 98    TrimExcess() -> Count: 50, Capacity: 50Count: 51, Capacity: 100    TrimExcess() -> Count: 51, Capacity: 51Count: 52, Capacity: 102    TrimExcess() -> Count: 52, Capacity: 52Count: 53, Capacity: 104    TrimExcess() -> Count: 53, Capacity: 53Count: 54, Capacity: 106    TrimExcess() -> Count: 54, Capacity: 54Count: 55, Capacity: 108    TrimExcess() -> Count: 55, Capacity: 55Count: 56, Capacity: 110    TrimExcess() -> Count: 56, Capacity: 56Count: 57, Capacity: 112    TrimExcess() -> Count: 57, Capacity: 57Count: 58, Capacity: 114    TrimExcess() -> Count: 58, Capacity: 58Count: 59, Capacity: 116    TrimExcess() -> Count: 59, Capacity: 59Count: 60, Capacity: 118    TrimExcess() -> Count: 60, Capacity: 60Count: 61, Capacity: 120    TrimExcess() -> Count: 61, Capacity: 61Count: 62, Capacity: 122    TrimExcess() -> Count: 62, Capacity: 62Count: 63, Capacity: 124    TrimExcess() -> Count: 63, Capacity: 63Count: 64, Capacity: 126    TrimExcess() -> Count: 64, Capacity: 64
View Code

发现并不是90%以后

System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity));int initCapacity = 100;List<string> l = new List<string>(initCapacity);System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity));int initCount = 100;for (int u = 0; u < initCount; ++u){    l.Add(u.ToString());}for (int u = 0; u < initCount; ++u){    System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity));    l.TrimExcess();    System.Diagnostics.Debug.WriteLine(string.Format("    TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity));    l.Remove(u.ToString());}
View Code

得到

default capacity is : 0constructe a list with capacity: 100Count: 100, Capacity: 100    TrimExcess() -> Count: 100, Capacity: 100Count: 99, Capacity: 100    TrimExcess() -> Count: 99, Capacity: 100Count: 98, Capacity: 100    TrimExcess() -> Count: 98, Capacity: 100Count: 97, Capacity: 100    TrimExcess() -> Count: 97, Capacity: 100Count: 96, Capacity: 100    TrimExcess() -> Count: 96, Capacity: 100Count: 95, Capacity: 100    TrimExcess() -> Count: 95, Capacity: 100Count: 94, Capacity: 100    TrimExcess() -> Count: 94, Capacity: 100Count: 93, Capacity: 100    TrimExcess() -> Count: 93, Capacity: 100Count: 92, Capacity: 100    TrimExcess() -> Count: 92, Capacity: 100Count: 91, Capacity: 100    TrimExcess() -> Count: 91, Capacity: 100Count: 90, Capacity: 100    TrimExcess() -> Count: 90, Capacity: 100Count: 89, Capacity: 100    TrimExcess() -> Count: 89, Capacity: 89Count: 88, Capacity: 89    TrimExcess() -> Count: 88, Capacity: 89Count: 87, Capacity: 89    TrimExcess() -> Count: 87, Capacity: 89Count: 86, Capacity: 89    TrimExcess() -> Count: 86, Capacity: 89Count: 85, Capacity: 89    TrimExcess() -> Count: 85, Capacity: 89Count: 84, Capacity: 89    TrimExcess() -> Count: 84, Capacity: 89Count: 83, Capacity: 89    TrimExcess() -> Count: 83, Capacity: 89Count: 82, Capacity: 89    TrimExcess() -> Count: 82, Capacity: 89Count: 81, Capacity: 89    TrimExcess() -> Count: 81, Capacity: 89Count: 80, Capacity: 89    TrimExcess() -> Count: 80, Capacity: 89Count: 79, Capacity: 89    TrimExcess() -> Count: 79, Capacity: 79Count: 78, Capacity: 79    TrimExcess() -> Count: 78, Capacity: 79Count: 77, Capacity: 79    TrimExcess() -> Count: 77, Capacity: 79Count: 76, Capacity: 79    TrimExcess() -> Count: 76, Capacity: 79Count: 75, Capacity: 79    TrimExcess() -> Count: 75, Capacity: 79Count: 74, Capacity: 79    TrimExcess() -> Count: 74, Capacity: 79Count: 73, Capacity: 79    TrimExcess() -> Count: 73, Capacity: 79Count: 72, Capacity: 79    TrimExcess() -> Count: 72, Capacity: 79Count: 71, Capacity: 79    TrimExcess() -> Count: 71, Capacity: 79Count: 70, Capacity: 79    TrimExcess() -> Count: 70, Capacity: 70Count: 69, Capacity: 70    TrimExcess() -> Count: 69, Capacity: 70Count: 68, Capacity: 70    TrimExcess() -> Count: 68, Capacity: 70Count: 67, Capacity: 70    TrimExcess() -> Count: 67, Capacity: 70Count: 66, Capacity: 70    TrimExcess() -> Count: 66, Capacity: 70Count: 65, Capacity: 70    TrimExcess() -> Count: 65, Capacity: 70Count: 64, Capacity: 70    TrimExcess() -> Count: 64, Capacity: 70Count: 63, Capacity: 70    TrimExcess() -> Count: 63, Capacity: 70Count: 62, Capacity: 70    TrimExcess() -> Count: 62, Capacity: 62Count: 61, Capacity: 62    TrimExcess() -> Count: 61, Capacity: 62Count: 60, Capacity: 62    TrimExcess() -> Count: 60, Capacity: 62Count: 59, Capacity: 62    TrimExcess() -> Count: 59, Capacity: 62Count: 58, Capacity: 62    TrimExcess() -> Count: 58, Capacity: 62Count: 57, Capacity: 62    TrimExcess() -> Count: 57, Capacity: 62Count: 56, Capacity: 62    TrimExcess() -> Count: 56, Capacity: 62Count: 55, Capacity: 62    TrimExcess() -> Count: 55, Capacity: 62Count: 54, Capacity: 62    TrimExcess() -> Count: 54, Capacity: 54Count: 53, Capacity: 54    TrimExcess() -> Count: 53, Capacity: 54Count: 52, Capacity: 54    TrimExcess() -> Count: 52, Capacity: 54Count: 51, Capacity: 54    TrimExcess() -> Count: 51, Capacity: 54Count: 50, Capacity: 54    TrimExcess() -> Count: 50, Capacity: 54Count: 49, Capacity: 54    TrimExcess() -> Count: 49, Capacity: 54Count: 48, Capacity: 54    TrimExcess() -> Count: 48, Capacity: 54Count: 47, Capacity: 54    TrimExcess() -> Count: 47, Capacity: 47Count: 46, Capacity: 47    TrimExcess() -> Count: 46, Capacity: 47Count: 45, Capacity: 47    TrimExcess() -> Count: 45, Capacity: 47Count: 44, Capacity: 47    TrimExcess() -> Count: 44, Capacity: 47Count: 43, Capacity: 47    TrimExcess() -> Count: 43, Capacity: 47Count: 42, Capacity: 47    TrimExcess() -> Count: 42, Capacity: 47Count: 41, Capacity: 47    TrimExcess() -> Count: 41, Capacity: 41Count: 40, Capacity: 41    TrimExcess() -> Count: 40, Capacity: 41Count: 39, Capacity: 41    TrimExcess() -> Count: 39, Capacity: 41Count: 38, Capacity: 41    TrimExcess() -> Count: 38, Capacity: 41Count: 37, Capacity: 41    TrimExcess() -> Count: 37, Capacity: 41Count: 36, Capacity: 41    TrimExcess() -> Count: 36, Capacity: 41Count: 35, Capacity: 41    TrimExcess() -> Count: 35, Capacity: 35Count: 34, Capacity: 35    TrimExcess() -> Count: 34, Capacity: 35Count: 33, Capacity: 35    TrimExcess() -> Count: 33, Capacity: 35Count: 32, Capacity: 35    TrimExcess() -> Count: 32, Capacity: 35Count: 31, Capacity: 35    TrimExcess() -> Count: 31, Capacity: 35Count: 30, Capacity: 35    TrimExcess() -> Count: 30, Capacity: 30Count: 29, Capacity: 30    TrimExcess() -> Count: 29, Capacity: 30Count: 28, Capacity: 30    TrimExcess() -> Count: 28, Capacity: 30Count: 27, Capacity: 30    TrimExcess() -> Count: 27, Capacity: 30Count: 26, Capacity: 30    TrimExcess() -> Count: 26, Capacity: 26Count: 25, Capacity: 26    TrimExcess() -> Count: 25, Capacity: 26Count: 24, Capacity: 26    TrimExcess() -> Count: 24, Capacity: 26Count: 23, Capacity: 26    TrimExcess() -> Count: 23, Capacity: 26Count: 22, Capacity: 26    TrimExcess() -> Count: 22, Capacity: 22Count: 21, Capacity: 22    TrimExcess() -> Count: 21, Capacity: 22Count: 20, Capacity: 22    TrimExcess() -> Count: 20, Capacity: 22Count: 19, Capacity: 22    TrimExcess() -> Count: 19, Capacity: 22Count: 18, Capacity: 22    TrimExcess() -> Count: 18, Capacity: 18Count: 17, Capacity: 18    TrimExcess() -> Count: 17, Capacity: 18Count: 16, Capacity: 18    TrimExcess() -> Count: 16, Capacity: 18Count: 15, Capacity: 18    TrimExcess() -> Count: 15, Capacity: 15Count: 14, Capacity: 15    TrimExcess() -> Count: 14, Capacity: 15Count: 13, Capacity: 15    TrimExcess() -> Count: 13, Capacity: 15Count: 12, Capacity: 15    TrimExcess() -> Count: 12, Capacity: 12Count: 11, Capacity: 12    TrimExcess() -> Count: 11, Capacity: 12Count: 10, Capacity: 12    TrimExcess() -> Count: 10, Capacity: 12Count: 9, Capacity: 12    TrimExcess() -> Count: 9, Capacity: 9Count: 8, Capacity: 9    TrimExcess() -> Count: 8, Capacity: 9Count: 7, Capacity: 9    TrimExcess() -> Count: 7, Capacity: 7Count: 6, Capacity: 7    TrimExcess() -> Count: 6, Capacity: 7Count: 5, Capacity: 7    TrimExcess() -> Count: 5, Capacity: 5Count: 4, Capacity: 5    TrimExcess() -> Count: 4, Capacity: 5Count: 3, Capacity: 5    TrimExcess() -> Count: 3, Capacity: 3Count: 2, Capacity: 3    TrimExcess() -> Count: 2, Capacity: 3Count: 1, Capacity: 3    TrimExcess() -> Count: 1, Capacity: 1
View Code

貌似条件是酱紫的

if( count<(int)Math.Floor((double)capacity*0.9) )

    do trim

 

我也不知道我要干嘛