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 : 0 constructe a list with capacity: 0 Count: 0, Capacity: 0 Count: 1, Capacity: 4 Count: 2, Capacity: 4 Count: 3, Capacity: 4 Count: 4, Capacity: 4 Count: 5, Capacity: 8 Count: 6, Capacity: 8 Count: 7, Capacity: 8 Count: 8, Capacity: 8 Count: 9, Capacity: 16 Count: 10, Capacity: 16 Count: 11, Capacity: 16 Count: 12, Capacity: 16 Count: 13, Capacity: 16 Count: 14, Capacity: 16 Count: 15, Capacity: 16 Count: 16, Capacity: 16 Count: 17, Capacity: 32 Count: 18, Capacity: 32 Count: 19, Capacity: 32 Count: 20, Capacity: 32 Count: 21, Capacity: 32 Count: 22, Capacity: 32 Count: 23, Capacity: 32 Count: 24, Capacity: 32 Count: 25, Capacity: 32 Count: 26, Capacity: 32 Count: 27, Capacity: 32 Count: 28, Capacity: 32 Count: 29, Capacity: 32 Count: 30, Capacity: 32 Count: 31, Capacity: 32 Count: 32, Capacity: 32 Count: 33, Capacity: 64 Count: 34, Capacity: 64 Count: 35, Capacity: 64 Count: 36, Capacity: 64 Count: 37, Capacity: 64 Count: 38, Capacity: 64 Count: 39, Capacity: 64 Count: 40, Capacity: 64 Count: 41, Capacity: 64 Count: 42, Capacity: 64 Count: 43, Capacity: 64 Count: 44, Capacity: 64 Count: 45, Capacity: 64 Count: 46, Capacity: 64 Count: 47, Capacity: 64 Count: 48, Capacity: 64 Count: 49, Capacity: 64 Count: 50, Capacity: 64 Count: 51, Capacity: 64 Count: 52, Capacity: 64 Count: 53, Capacity: 64 Count: 54, Capacity: 64 Count: 55, Capacity: 64 Count: 56, Capacity: 64 Count: 57, Capacity: 64 Count: 58, Capacity: 64 Count: 59, Capacity: 64 Count: 60, Capacity: 64 Count: 61, Capacity: 64 Count: 62, Capacity: 64 Count: 63, Capacity: 64 Count: 64, Capacity: 64
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 : 0 constructe a list with capacity: 0 Count: 0, Capacity: 0 TrimExcess() -> Count: 0, Capacity: 0 Count: 1, Capacity: 4 TrimExcess() -> Count: 1, Capacity: 1 Count: 2, Capacity: 2 TrimExcess() -> Count: 2, Capacity: 2 Count: 3, Capacity: 4 TrimExcess() -> Count: 3, Capacity: 4 Count: 4, Capacity: 4 TrimExcess() -> Count: 4, Capacity: 4 Count: 5, Capacity: 8 TrimExcess() -> Count: 5, Capacity: 5 Count: 6, Capacity: 10 TrimExcess() -> Count: 6, Capacity: 6 Count: 7, Capacity: 12 TrimExcess() -> Count: 7, Capacity: 7 Count: 8, Capacity: 14 TrimExcess() -> Count: 8, Capacity: 8 Count: 9, Capacity: 16 TrimExcess() -> Count: 9, Capacity: 9 Count: 10, Capacity: 18 TrimExcess() -> Count: 10, Capacity: 10 Count: 11, Capacity: 20 TrimExcess() -> Count: 11, Capacity: 11 Count: 12, Capacity: 22 TrimExcess() -> Count: 12, Capacity: 12 Count: 13, Capacity: 24 TrimExcess() -> Count: 13, Capacity: 13 Count: 14, Capacity: 26 TrimExcess() -> Count: 14, Capacity: 14 Count: 15, Capacity: 28 TrimExcess() -> Count: 15, Capacity: 15 Count: 16, Capacity: 30 TrimExcess() -> Count: 16, Capacity: 16 Count: 17, Capacity: 32 TrimExcess() -> Count: 17, Capacity: 17 Count: 18, Capacity: 34 TrimExcess() -> Count: 18, Capacity: 18 Count: 19, Capacity: 36 TrimExcess() -> Count: 19, Capacity: 19 Count: 20, Capacity: 38 TrimExcess() -> Count: 20, Capacity: 20 Count: 21, Capacity: 40 TrimExcess() -> Count: 21, Capacity: 21 Count: 22, Capacity: 42 TrimExcess() -> Count: 22, Capacity: 22 Count: 23, Capacity: 44 TrimExcess() -> Count: 23, Capacity: 23 Count: 24, Capacity: 46 TrimExcess() -> Count: 24, Capacity: 24 Count: 25, Capacity: 48 TrimExcess() -> Count: 25, Capacity: 25 Count: 26, Capacity: 50 TrimExcess() -> Count: 26, Capacity: 26 Count: 27, Capacity: 52 TrimExcess() -> Count: 27, Capacity: 27 Count: 28, Capacity: 54 TrimExcess() -> Count: 28, Capacity: 28 Count: 29, Capacity: 56 TrimExcess() -> Count: 29, Capacity: 29 Count: 30, Capacity: 58 TrimExcess() -> Count: 30, Capacity: 30 Count: 31, Capacity: 60 TrimExcess() -> Count: 31, Capacity: 31 Count: 32, Capacity: 62 TrimExcess() -> Count: 32, Capacity: 32 Count: 33, Capacity: 64 TrimExcess() -> Count: 33, Capacity: 33 Count: 34, Capacity: 66 TrimExcess() -> Count: 34, Capacity: 34 Count: 35, Capacity: 68 TrimExcess() -> Count: 35, Capacity: 35 Count: 36, Capacity: 70 TrimExcess() -> Count: 36, Capacity: 36 Count: 37, Capacity: 72 TrimExcess() -> Count: 37, Capacity: 37 Count: 38, Capacity: 74 TrimExcess() -> Count: 38, Capacity: 38 Count: 39, Capacity: 76 TrimExcess() -> Count: 39, Capacity: 39 Count: 40, Capacity: 78 TrimExcess() -> Count: 40, Capacity: 40 Count: 41, Capacity: 80 TrimExcess() -> Count: 41, Capacity: 41 Count: 42, Capacity: 82 TrimExcess() -> Count: 42, Capacity: 42 Count: 43, Capacity: 84 TrimExcess() -> Count: 43, Capacity: 43 Count: 44, Capacity: 86 TrimExcess() -> Count: 44, Capacity: 44 Count: 45, Capacity: 88 TrimExcess() -> Count: 45, Capacity: 45 Count: 46, Capacity: 90 TrimExcess() -> Count: 46, Capacity: 46 Count: 47, Capacity: 92 TrimExcess() -> Count: 47, Capacity: 47 Count: 48, Capacity: 94 TrimExcess() -> Count: 48, Capacity: 48 Count: 49, Capacity: 96 TrimExcess() -> Count: 49, Capacity: 49 Count: 50, Capacity: 98 TrimExcess() -> Count: 50, Capacity: 50 Count: 51, Capacity: 100 TrimExcess() -> Count: 51, Capacity: 51 Count: 52, Capacity: 102 TrimExcess() -> Count: 52, Capacity: 52 Count: 53, Capacity: 104 TrimExcess() -> Count: 53, Capacity: 53 Count: 54, Capacity: 106 TrimExcess() -> Count: 54, Capacity: 54 Count: 55, Capacity: 108 TrimExcess() -> Count: 55, Capacity: 55 Count: 56, Capacity: 110 TrimExcess() -> Count: 56, Capacity: 56 Count: 57, Capacity: 112 TrimExcess() -> Count: 57, Capacity: 57 Count: 58, Capacity: 114 TrimExcess() -> Count: 58, Capacity: 58 Count: 59, Capacity: 116 TrimExcess() -> Count: 59, Capacity: 59 Count: 60, Capacity: 118 TrimExcess() -> Count: 60, Capacity: 60 Count: 61, Capacity: 120 TrimExcess() -> Count: 61, Capacity: 61 Count: 62, Capacity: 122 TrimExcess() -> Count: 62, Capacity: 62 Count: 63, Capacity: 124 TrimExcess() -> Count: 63, Capacity: 63 Count: 64, Capacity: 126 TrimExcess() -> Count: 64, Capacity: 64
发现并不是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()); }
得到
default capacity is : 0 constructe a list with capacity: 100 Count: 100, Capacity: 100 TrimExcess() -> Count: 100, Capacity: 100 Count: 99, Capacity: 100 TrimExcess() -> Count: 99, Capacity: 100 Count: 98, Capacity: 100 TrimExcess() -> Count: 98, Capacity: 100 Count: 97, Capacity: 100 TrimExcess() -> Count: 97, Capacity: 100 Count: 96, Capacity: 100 TrimExcess() -> Count: 96, Capacity: 100 Count: 95, Capacity: 100 TrimExcess() -> Count: 95, Capacity: 100 Count: 94, Capacity: 100 TrimExcess() -> Count: 94, Capacity: 100 Count: 93, Capacity: 100 TrimExcess() -> Count: 93, Capacity: 100 Count: 92, Capacity: 100 TrimExcess() -> Count: 92, Capacity: 100 Count: 91, Capacity: 100 TrimExcess() -> Count: 91, Capacity: 100 Count: 90, Capacity: 100 TrimExcess() -> Count: 90, Capacity: 100 Count: 89, Capacity: 100 TrimExcess() -> Count: 89, Capacity: 89 Count: 88, Capacity: 89 TrimExcess() -> Count: 88, Capacity: 89 Count: 87, Capacity: 89 TrimExcess() -> Count: 87, Capacity: 89 Count: 86, Capacity: 89 TrimExcess() -> Count: 86, Capacity: 89 Count: 85, Capacity: 89 TrimExcess() -> Count: 85, Capacity: 89 Count: 84, Capacity: 89 TrimExcess() -> Count: 84, Capacity: 89 Count: 83, Capacity: 89 TrimExcess() -> Count: 83, Capacity: 89 Count: 82, Capacity: 89 TrimExcess() -> Count: 82, Capacity: 89 Count: 81, Capacity: 89 TrimExcess() -> Count: 81, Capacity: 89 Count: 80, Capacity: 89 TrimExcess() -> Count: 80, Capacity: 89 Count: 79, Capacity: 89 TrimExcess() -> Count: 79, Capacity: 79 Count: 78, Capacity: 79 TrimExcess() -> Count: 78, Capacity: 79 Count: 77, Capacity: 79 TrimExcess() -> Count: 77, Capacity: 79 Count: 76, Capacity: 79 TrimExcess() -> Count: 76, Capacity: 79 Count: 75, Capacity: 79 TrimExcess() -> Count: 75, Capacity: 79 Count: 74, Capacity: 79 TrimExcess() -> Count: 74, Capacity: 79 Count: 73, Capacity: 79 TrimExcess() -> Count: 73, Capacity: 79 Count: 72, Capacity: 79 TrimExcess() -> Count: 72, Capacity: 79 Count: 71, Capacity: 79 TrimExcess() -> Count: 71, Capacity: 79 Count: 70, Capacity: 79 TrimExcess() -> Count: 70, Capacity: 70 Count: 69, Capacity: 70 TrimExcess() -> Count: 69, Capacity: 70 Count: 68, Capacity: 70 TrimExcess() -> Count: 68, Capacity: 70 Count: 67, Capacity: 70 TrimExcess() -> Count: 67, Capacity: 70 Count: 66, Capacity: 70 TrimExcess() -> Count: 66, Capacity: 70 Count: 65, Capacity: 70 TrimExcess() -> Count: 65, Capacity: 70 Count: 64, Capacity: 70 TrimExcess() -> Count: 64, Capacity: 70 Count: 63, Capacity: 70 TrimExcess() -> Count: 63, Capacity: 70 Count: 62, Capacity: 70 TrimExcess() -> Count: 62, Capacity: 62 Count: 61, Capacity: 62 TrimExcess() -> Count: 61, Capacity: 62 Count: 60, Capacity: 62 TrimExcess() -> Count: 60, Capacity: 62 Count: 59, Capacity: 62 TrimExcess() -> Count: 59, Capacity: 62 Count: 58, Capacity: 62 TrimExcess() -> Count: 58, Capacity: 62 Count: 57, Capacity: 62 TrimExcess() -> Count: 57, Capacity: 62 Count: 56, Capacity: 62 TrimExcess() -> Count: 56, Capacity: 62 Count: 55, Capacity: 62 TrimExcess() -> Count: 55, Capacity: 62 Count: 54, Capacity: 62 TrimExcess() -> Count: 54, Capacity: 54 Count: 53, Capacity: 54 TrimExcess() -> Count: 53, Capacity: 54 Count: 52, Capacity: 54 TrimExcess() -> Count: 52, Capacity: 54 Count: 51, Capacity: 54 TrimExcess() -> Count: 51, Capacity: 54 Count: 50, Capacity: 54 TrimExcess() -> Count: 50, Capacity: 54 Count: 49, Capacity: 54 TrimExcess() -> Count: 49, Capacity: 54 Count: 48, Capacity: 54 TrimExcess() -> Count: 48, Capacity: 54 Count: 47, Capacity: 54 TrimExcess() -> Count: 47, Capacity: 47 Count: 46, Capacity: 47 TrimExcess() -> Count: 46, Capacity: 47 Count: 45, Capacity: 47 TrimExcess() -> Count: 45, Capacity: 47 Count: 44, Capacity: 47 TrimExcess() -> Count: 44, Capacity: 47 Count: 43, Capacity: 47 TrimExcess() -> Count: 43, Capacity: 47 Count: 42, Capacity: 47 TrimExcess() -> Count: 42, Capacity: 47 Count: 41, Capacity: 47 TrimExcess() -> Count: 41, Capacity: 41 Count: 40, Capacity: 41 TrimExcess() -> Count: 40, Capacity: 41 Count: 39, Capacity: 41 TrimExcess() -> Count: 39, Capacity: 41 Count: 38, Capacity: 41 TrimExcess() -> Count: 38, Capacity: 41 Count: 37, Capacity: 41 TrimExcess() -> Count: 37, Capacity: 41 Count: 36, Capacity: 41 TrimExcess() -> Count: 36, Capacity: 41 Count: 35, Capacity: 41 TrimExcess() -> Count: 35, Capacity: 35 Count: 34, Capacity: 35 TrimExcess() -> Count: 34, Capacity: 35 Count: 33, Capacity: 35 TrimExcess() -> Count: 33, Capacity: 35 Count: 32, Capacity: 35 TrimExcess() -> Count: 32, Capacity: 35 Count: 31, Capacity: 35 TrimExcess() -> Count: 31, Capacity: 35 Count: 30, Capacity: 35 TrimExcess() -> Count: 30, Capacity: 30 Count: 29, Capacity: 30 TrimExcess() -> Count: 29, Capacity: 30 Count: 28, Capacity: 30 TrimExcess() -> Count: 28, Capacity: 30 Count: 27, Capacity: 30 TrimExcess() -> Count: 27, Capacity: 30 Count: 26, Capacity: 30 TrimExcess() -> Count: 26, Capacity: 26 Count: 25, Capacity: 26 TrimExcess() -> Count: 25, Capacity: 26 Count: 24, Capacity: 26 TrimExcess() -> Count: 24, Capacity: 26 Count: 23, Capacity: 26 TrimExcess() -> Count: 23, Capacity: 26 Count: 22, Capacity: 26 TrimExcess() -> Count: 22, Capacity: 22 Count: 21, Capacity: 22 TrimExcess() -> Count: 21, Capacity: 22 Count: 20, Capacity: 22 TrimExcess() -> Count: 20, Capacity: 22 Count: 19, Capacity: 22 TrimExcess() -> Count: 19, Capacity: 22 Count: 18, Capacity: 22 TrimExcess() -> Count: 18, Capacity: 18 Count: 17, Capacity: 18 TrimExcess() -> Count: 17, Capacity: 18 Count: 16, Capacity: 18 TrimExcess() -> Count: 16, Capacity: 18 Count: 15, Capacity: 18 TrimExcess() -> Count: 15, Capacity: 15 Count: 14, Capacity: 15 TrimExcess() -> Count: 14, Capacity: 15 Count: 13, Capacity: 15 TrimExcess() -> Count: 13, Capacity: 15 Count: 12, Capacity: 15 TrimExcess() -> Count: 12, Capacity: 12 Count: 11, Capacity: 12 TrimExcess() -> Count: 11, Capacity: 12 Count: 10, Capacity: 12 TrimExcess() -> Count: 10, Capacity: 12 Count: 9, Capacity: 12 TrimExcess() -> Count: 9, Capacity: 9 Count: 8, Capacity: 9 TrimExcess() -> Count: 8, Capacity: 9 Count: 7, Capacity: 9 TrimExcess() -> Count: 7, Capacity: 7 Count: 6, Capacity: 7 TrimExcess() -> Count: 6, Capacity: 7 Count: 5, Capacity: 7 TrimExcess() -> Count: 5, Capacity: 5 Count: 4, Capacity: 5 TrimExcess() -> Count: 4, Capacity: 5 Count: 3, Capacity: 5 TrimExcess() -> Count: 3, Capacity: 3 Count: 2, Capacity: 3 TrimExcess() -> Count: 2, Capacity: 3 Count: 1, Capacity: 3 TrimExcess() -> Count: 1, Capacity: 1
貌似条件是酱紫的
if( count<(int)Math.Floor((double)capacity*0.9) )
do trim
我也不知道我要干嘛