《Advanced .NET Debugging》 读书笔记 Listing 5-5: LOH的简单示例
时间:2011-01-06 来源:李志鹏
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Advanced.NET.Debugging.Chapter5
{
class LOH
{
static void Main(string[] args)
{
LOH l = new LOH();
l.Run();
}
public void Run()
{
byte[] b = null;
Console.WriteLine("Press any key to allocate on LOH");
Console.ReadKey();
b = new byte[100000];
Console.WriteLine("Press any key to GC");
Console.ReadKey();
b = null;
GC.Collect();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
在程序中分配了一个包含10w个元素的数组,此时它会被分配到LOH上。可以通过WinDbg验证:
1. 在WinDbg中载入05LOH.exe
2. 执行.symfix
3. 在程序运行到分配内存之前,执行 !eeheap -gc结果如下:
此时可以找到LOH从0x0000000012151000开始
4. 执行 !DumpHeap –startatlowerbound 0x00000000012151000:
可见此时LOH上只是零散分布了一些对象。
5. 在分配对象以后,再次执行 !DumpHeap –startatlowerbound 0x00000000012151000:
可见在LOH上该10w字节长度的对象占据的大小为100024字节。