Wednesday, October 13, 2010

Windbg tips, Dump all Objects with the same type loop with .foreach

Case, there are a lot Memory get used by some objects with the same type.  let’s say the type is String here, how can we dump all those strings.

how to, list all those object ids, and run DumpObject.   .for each

here is a Demo.

static void Main(string[] args)
      {
          string[] buffer = new string[10];
          for (int i = 0; i < 10; i++)
          {
              //big strings
              buffer[i] = new string('-', 20*1024);
          }
          Console.ReadLine();
      }

//List all those string whose size is over 20K

!DumpHeap -type System.String -min 20000

0:004> !DumpHeap -type System.String -min 20000
Address       MT     Size
012d93bc 793308ec    32180    
013155e4 793308ec    45832    
0133d5a8 793308ec    32180    
013597c4 793308ec    40980    
013637d8 793308ec    40980    
0136d7ec 793308ec    40980    
01377800 793308ec    40980    
01381814 793308ec    40980    
0138b828 793308ec    40980    
0139583c 793308ec    40980    
0139f850 793308ec    40980    
013a9864 793308ec    40980    
013b3878 793308ec    40980


the address Column displays the ddress for each string. Let me pick up the red one.

0:004> !do 013b3878
Name: System.String
MethodTable: 793308ec
EEClass: 790ed64c
Size: 40978(0xa012) bytes
(C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
79332b38  4000096        4         System.Int32  1 instance    20481 m_arrayLength
79332b38  4000097        8         System.Int32  1 instance    20480 m_stringLength
793315cc  4000098        c          System.Char  1 instance       2d m_firstChar
793308ec  4000099       10        System.String  0   shared   static Empty
    >> Domain:Value  001599c8:012d1198 <<
7933151c  400009a       14        System.Char[]  0   shared   static WhitespaceChars
    >> Domain:Value  001599c8:012d18cc <<

If you want to List all the strings , only display ID.

0:004> !DumpHeap -type System.String -min 20000 -short
012d93bc
013155e4
0133d5a8
013597c4
013637d8
0136d7ec
01377800
01381814
0138b828
0139583c
0139f850
013a9864
013b3878

 

then run loop .foreach.

.foreach (address  {!DumpHeap -type System.String -min 20000 -short }) {!do ${address} }
//will display all strings

No comments:

 
Locations of visitors to this page