WinDbg实操一(查看.net对象)

查看.net对象

测试代码
 
using System;
using System.Collections.Generic;

namespace Demo1_ObjectGc
{
internal class Program
{
private static List _values = new List();
static void Main(string[] args)
{
for(int i = 0;i< 10000;i++)
{
_values.Add(new A() { ValueB = new B { Value = i} });
}
Console.WriteLine("运行结束,请开始分析");
Console.ReadLine();
}
}
public class A
{
public B ValueB { get; set; }
}
public class B
{
public int Value { get; set; }
}
}

0:006> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
0:006> .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\SOS.dll
0:006> .loadby sos clr
0:006> .load D:\WinDbgEx\SOSEx\x86\sosex.dll
查看堆中的对象类型和大小
0:006> !DumpHeap -stat
Statistics:
      MT    Count    TotalSize Class Name
7b3cd594        1           12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
7b3ccabc        1           12 System.Security.HostSecurityManager
7b3cc050        1           12 System.Collections.Generic.ObjectEqualityComparer`1[[System.Type, mscorlib]]
7b3cca40        1           16 System.Security.Policy.Evidence+EvidenceLockHolder
7b3c9fd8        1           16 System.Security.Policy.AssemblyEvidenceFactory
7b38d8b8        1           16 System.IO.TextReader+SyncTextReader
7b3c9f20        1           20 Microsoft.Win32.SafeHandles.SafePEFileHandle
7b3612f4        1           20 System.IO.Stream+NullStream
7b360ecc        1           20 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
7b360e7c        1           20 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
7b360de8        1           20 System.Text.InternalEncoderBestFitFallback
7b3cd28c        1           24 System.Version
7b360fa8        1           24 System.IO.TextWriter+SyncTextWriter
7b360e38        1           24 System.Text.InternalDecoderBestFitFallback
011d4f0c        1           24 System.Collections.Generic.List`1[[Demo1_ObjectGc.A, Demo1_ObjectGc]]
7b3cceb4        1           28 System.Text.UTF8Encoding+UTF8EncodingSealed
7b3caeec        1           28 System.SharedStatics
7b38f4c8        1           28 System.Text.DBCSCodePageEncoding+DBCSDecoder
7b3610e4        1           28 Microsoft.Win32.Win32Native+InputRecord
7b360a94        1           28 System.Text.EncoderNLS
7b3ccd54        2           32 System.Text.DecoderReplacementFallback
7b3ccd04        2           32 System.Text.EncoderReplacementFallback
7b3ccb30        1           32 System.Text.UnicodeEncoding
7b3c9f80        1           32 System.Security.Policy.PEFileEvidenceFactory
7b3cb60c        1           36 System.Security.PermissionSet
7b3cce28        2           40 Microsoft.Win32.SafeHandles.SafeFileHandle
7b3cb588        1           40 System.Security.Policy.Evidence
7b3cc91c        1           44 System.Threading.ReaderWriterLock
7b3676d4        1           44 System.Text.InternalEncoderBestFitFallbackBuffer
7b3cd3cc        1           48 System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Globalization.CultureData, mscorlib]]
7b3cd044        1           48 System.Collections.Hashtable+bucket[]
7b3cc54c        4           48 System.Int32
7b3cb760        1           48 System.Collections.Generic.Dictionary`2[[System.Type, mscorlib],[System.Security.Policy.EvidenceTypeDescriptor, mscorlib]]
7b3c9e80        4           48 System.UInt16
7b3ccff4        1           52 System.Collections.Hashtable
7b3cc098        1           52 System.Type[]
7b3cb52c        1           52 System.Threading.Thread
7b3cd378        2           56 System.Text.StringBuilder
7b3cc964        2           56 System.Reflection.RuntimeAssembly
7b36113c        2           56 System.IO.__ConsoleStream
7b3cdab4        1           60 System.IO.StreamWriter
7b3cd6c8        1           60 System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Globalization.CultureData, mscorlib]][]
7b3d33ac        1           64 System.IO.StreamReader
7b3cb2b0        1           68 System.AppDomainSetup
7b3cd0d4        1           72 System.IO.UnmanagedMemoryStream
7b38f93c        1           76 System.Text.DBCSCodePageEncoding
7b3cd338        1           84 System.Globalization.CalendarData
7b3cadd4        1           84 System.ExecutionEngineException
7b3cad90        1           84 System.StackOverflowException
7b3cad48        1           84 System.OutOfMemoryException
7b3cacf4        1           84 System.Exception
7b3cae50        8           96 System.Object
7b3cd2f4        1          104 System.Globalization.CalendarData[]
7b3caf2c        1          112 System.AppDomain
7b3cd80c        1          132 System.Globalization.NumberFormatInfo
7b3cd1a0        2          144 System.Globalization.CultureInfo
7b3cae18        2          168 System.Threading.ThreadAbortException
012f7338       44          456      Free
7b3cc778        3          468 System.Collections.Generic.Dictionary`2+Entry[[System.Type, mscorlib],[System.Security.Policy.EvidenceTypeDescriptor, mscorlib]][]
7b3cc510       11          564 System.Int32[]
7b3cb490       18          608 System.String[]
7b3cd258        2          616 System.Globalization.CultureData
7b3cbc4c       26          728 System.RuntimeType
7b3cdd04        3          806 System.Byte[]
7b3cb37c       10         2984 System.Char[]
7b3cac04      166         5606 System.String
7b3caea4        6        18016 System.Object[]
011d5400    10000       120000 Demo1_ObjectGc.B
011d4ebc    10000       120000 Demo1_ObjectGc.A
011d5300       14       131224 Demo1_ObjectGc.A[]
Total 20381 objects
根据类型查找存活对象
0:006> !DumpHeap -live -mt 011d4ebc
 Address       MT     Size
030e23fc 011d4ebc       12     
030e2430 011d4ebc       12     
030e2448 011d4ebc       12     
0313ce38 011d4ebc       12     
0313ce50 011d4ebc       12     
0313ce68 011d4ebc       12     
0313ce80 011d4ebc       12     
0313ce98 011d4ebc       12     
0313ceb0 011d4ebc       12     
0313cec8 011d4ebc       12     
0313cee0 011d4ebc       12     
0313cef8 011d4ebc       12     
0313cf10 011d4ebc       12     
0313cf28 011d4ebc       12     
0313cf40 011d4ebc       12     
0313cf58 011d4ebc       12     
0313cf70 011d4ebc       12     
0313cf88 011d4ebc       12     

Statistics:
      MT    Count    TotalSize Class Name
011d4ebc    10000       120000 Demo1_ObjectGc.A
Total 10000 objects
查看某个对象找出GCRoot
0:006> !GCRoot 0313cf88
HandleTable:
    011b13ec (pinned handle)
    -> 040e3670 System.Object[]
    -> 030e23b4 System.Collections.Generic.List`1[[Demo1_ObjectGc.A, Demo1_ObjectGc]]
    -> 031225f0 Demo1_ObjectGc.A[]
    -> 0313cf88 Demo1_ObjectGc.A

Found 1 unique roots (run '!GCRoot -all' to see all roots).
查看对象
0:006> !DumpObj 0313cf88
Name:        Demo1_ObjectGc.A
MethodTable: 011d4ebc
EEClass:     011d1344
Size:        12(0xc) bytes
File:        E:\workspcace\WinDbg\WinDbgStudy\Demo1_ObjectGc\bin\Debug\Demo1_ObjectGc.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
011d5400  4000002        4     Demo1_ObjectGc.B  0 instance 0313cf94 <ValueB>k__BackingField

0:006> !do 0313cf94 
Name:        Demo1_ObjectGc.B
MethodTable: 011d5400
EEClass:     011d14c8
Size:        12(0xc) bytes
File:        E:\workspcace\WinDbg\WinDbgStudy\Demo1_ObjectGc\bin\Debug\Demo1_ObjectGc.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
7b3cc54c  4000003        4         System.Int32  1 instance     9999 <Value>k__BackingField
posted @ 2024-04-07 12:31  leafroc  阅读(71)  评论(0编辑  收藏  举报