How to iterate over objects present in a managed .NET heap?

Is there a way in API.NET to iterate on managed objects that are in a managed heap?

We would like to add a routine at some points in our program, which checks for the presence of some objects in a managed heap.

+2
garbage-collection
source share
2 answers

To do this, you need to use the debugging / profiling API , which I would not recommend for a "normal" application.

+3
source share
  • Install "Debugging Tools for Windows."

    but. Link: http://www.microsoft.com/whdc/devtools/debugging/default.mspx

    b. You will use WinDbg.EXE, your own Windows debugger and SOS.DLL (aka "Strike Son"), a WinDbg extension for managed code.

    from. The default installation location is "C: \ Program Files \ Debugging Tools for Windows".

  • Run the managed process.

  • Launch WinDbg and click "File → Attach to Process ..."

  • Select your process from the list.

  • WinDbg will automatically terminate (terminate) when it joins your process.

  • Type ".load sos.dll" (yes, with the previous ".) At the WinDbgs command line to download Son of Strike.

  • Enter "! Help" to view a list of commands / functions that Son of Strike offers.

  • Enter "! Traverseheap -xml heap.xml" to upload a bunch of your process in heap.xml to the WinDbgs directory (for example, C: \ Program Files \ Debugging Tools for Windows).

    but. Moving and dumping the heap to a file can take a very long time. WinDbg will send "BUSY" to its status and print "." - s to indicate progress.

    b. In general, heap.xml is structured as follows:

    <Type Identifiers>

    ...

    <Objects>

    Each class (type) contains an identifier, and each object has its own type identifier, managed address, and size.

  • Use findstr (Command Prompt) against heap.xml to grep your path through the heap, and WinDbg to dump objects.

    but. Example: find the identifier of the type of the DataTable class.

    • findstr "DataTable" heap.xml

      Output: <type id = "1002" name = "System.Data.DataTable" / ">

    b. Example: find all DataTable objects.

    • findstr "typeid = \" 1002 \ "" heap.xml

      Output: <object address = "0x0137ECD8" typeid = "1002" size = "296">

    from. Example: Dump of a DataTable.

    • (In WinDbg)! dumpobj 0137ecd8

    e. Example: Dump a member object. The address of the member object is stored in the "Value" field of members containing the dump object.

    • ! dumpobj <"Value">

.

Add, if you want to track large object distributions, here's how to break the CLR into a LOH distribution. When breaking, press "k" to see the column.

Launch WinDbg and attach as shown below.

  • Add the Microsoft symbol server to the symbol path:
  • Update characters:
    • .reload
  • Reboot the runtime:
    • .reload / f mscorwks.dll
  • Break on UP distribution:
    • bp mscorwks! WKS :: gc_heap :: allocate_large_object
  • MP distribution break:
    • bp mscorwks! SVR :: gc_heap :: allocate_large_object
  • Confirm breakpoints:
    • bl
  • Summary:
    • g
+6
source share

All Articles