使用windbg导出dump文件
.dump /ma D: estdump.dmp
gchandles命令列出句柄,同时列出句柄引用的对象,演示代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Data; using System.Runtime.InteropServices; public class Example { static void Main(string[] args) { int[] arry = new int[10]; for (int i = 0; i < 10; i++) { arry[i] = i; } GCHandle h1 = GCHandle.Alloc(arry, GCHandleType.Pinned); Console.ReadKey(); } }
windbg附加后,运行至ReadKey处,运行GCHandles
0:000> !gchandles
Handle Type Object Size Data Type
00000000000a13a8 Strong 00000000027f2070 64 System.Security.PermissionSet
00000000000a13b0 Strong 00000000027f1440 48 System.SharedStatics
00000000000a13b8 Strong 00000000027f1368 160 System.Threading.ThreadAbortException
00000000000a13c0 Strong 00000000027f12c8 160 System.Threading.ThreadAbortException
00000000000a13c8 Strong 00000000027f1228 160 System.ExecutionEngineException
00000000000a13d0 Strong 00000000027f1188 160 System.StackOverflowException
00000000000a13d8 Strong 00000000027f10e8 160 System.OutOfMemoryException
00000000000a13e0 Strong 00000000027f1048 160 System.Exception
00000000000a13f8 Strong 00000000027f1538 216 System.AppDomain
00000000000a17d0 Pinned 00000000027f2ca8 64 System.Int32[]
00000000000a17d8 Pinned 00000000127f5710 16352 System.Object[]
00000000000a17e0 Pinned 00000000127f36f0 8192 System.Object[]
00000000000a17e8 Pinned 00000000127f32b0 1056 System.Object[]
00000000000a17f0 Pinned 00000000027f1408 24 System.Object
00000000000a17f8 Pinned 00000000127f1038 8792 System.Object[]
此时,可以看到对象是00000000027f2ca8 ,持有他的句柄是00000000000a17d0
看下句柄内容:
0:000> dd 00000000000a17d0
00000000`000a17d0 027f2ca8 00000000 127f5710 00000000
00000000`000a17e0 127f36f0 00000000 127f32b0 00000000
00000000`000a17f0 027f1408 00000000 127f1038 00000000
00000000`000a1800 00000000 00000000 00000000 00000000
00000000`000a1810 00000000 00000000 00000000 00000000
00000000`000a1820 00000000 00000000 00000000 00000000
00000000`000a1830 00000000 00000000 00000000 00000000
00000000`000a1840 00000000 00000000 00000000 00000000
句柄中的前8个字节正好是持有的对象;
补充:Pined句柄表示被固定不会被移动的对象。