《Advanced .NET Debugging》 读书笔记 Listing 5-1: 简单的内存分配
时间:2010-12-30 来源:李志鹏
using System;
using System.Text;
using System.Runtime.Remoting;
namespace Advanced.NET.Debugging.Chapter5
{
class Name
{
private string first;
private string last;
public string First { get { return first; } }
public string Last { get { return last; } }
public Name(string f, string l)
{
first = f; last = l;
}
}
class SimpleAlloc
{
static void Main(string[] args)
{
Name name = null;
Console.WriteLine("Press any key to allocate memory");
Console.ReadKey();
name = new Name("Mario", "Hewardt");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
1. 在WinDbg内载入05SimpleAlloc.exe
2. 在程序要求按键继续时,执行 .loadby sos.dll mscorwks
3. 执行 !dumpheap此时可以看到heap内的对象分布情况
4. 执行 !dumpheap –type Advanced.NET.Debugging.Chapter5.Name 此时可以看到该类型在heap上的分布情况(此时没有实例化Name对象):
5. 执行 g。
6. 当程序再次要求按键继续时,执行 !dumpheap –type Advanced.NET.Debugging.Chapter5.Name ,可以看到实例化了一个对象: