110,538
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
List<int> list = new List<int>(80000/4);
for (int i = 0; i < 80000 / 4; i++) l.Add(3);
object o = list;
// break point 1
list.AddRange(list);
// break point 2
}
!clrstack -a
LOCALS:
<CLR reg> = 0x01e61c58
!dumpobj 0x01e61c58 // at break point 1
Name: System.Collections.Generic.List`1[[System.Int32, mscorlib]]
MT Field Offset Type VT Attr Value Name
7912d7c0 40009c7 4 System.Int32[] 0 instance 01e61c70 _items <----1 in GC generation 2
79102290 40009c8 c System.Int32 1 instance 20000 _size
79102290 40009c9 10 System.Int32 1 instance 20000 _version
790fd0f0 40009ca 8 System.Object 0 instance 00000000 _syncRoot
7912d7c0 40009cb 0 System.Int32[] 0 shared static _emptyArray
!dumpobj 0x01e61c58 // at break point 2
Name: System.Collections.Generic.List`1[[System.Int32, mscorlib]]
MT Field Offset Type VT Attr Value Name
7912d7c0 40009c7 4 System.Int32[] 0 instance 02e63030 _items <----2 in Large Object heap
79102290 40009c8 c System.Int32 1 instance 40000 _size
79102290 40009c9 10 System.Int32 1 instance 20001 _version
790fd0f0 40009ca 8 System.Object 0 instance 00000000 _syncRoot
7912d7c0 40009cb 0 System.Int32[] 0 shared static _emptyArray
!eeheap
=======================================
Number of GC Heaps: 1
generation 0 starts at 0x01e61018
generation 1 starts at 0x01e6100c
generation 2 starts at 0x01e61000 <------1
Large object heap starts at 0x02e61000 <------2
class List
{
int length;
int capacity = 8;
int[] buf = new int[8];
public void Add(int val)
{
if( length == capacity - 1 )
{
capacity *= 2; // 数组扩大一倍
int[] newBuf = new int[ capacity ]; // 创建新对象
Array.Copy(newBuf, buf, length); // 拷贝数据
buf = newBuf; // 删除旧对象
}
buf[length++] = val;
}
}