Thanks to the good people at ShinyStat that provides me with free statistics of my blog, I am able to see all referrers including those that come from search engines and even see the keywords they have used to search and find this blog.

I’ve looked just now at the statistics and saw one combination of keywords that interested me. It was something like this: “!dumpheap parameters”

This means that someone was looking for some help on the parameters of the !dumpheap command, so I thought I’ll add some i.nformation about that.

What is !dumpheap?

!dumpheap is a command from the SOS extension that dumps the content of the managed heap.
You can get all the addresses and some additional information (as we will see) on all managed objects currently alive on the heap.

From the last two versions of WinDbg SOS was actually replaced by PSSCOR which has a good help system. For most commands you can simply type “!help commandName“, for example, “!help dumpheap” and you will get a details help on the parameters and how to use.

!dumpheap Parameters:

  • -stat – Outputs only the statistical summary of all types of objects on the heap, their count and their own size (without references)
  • -nostrings – Exclude the output of strings (when not using -stat).
  • -gen X – Outputs only objects that belong to generation X where X can have the following values: for 1.1 – 0, 1, 2 and 3 for large objects (objects larger than 85Kb without their references). For 1.0 everything expect use -1 instead of 3.
  • -min X – Ignores objects that are less than X (where X is a number in bytes).
  • -max X – Ignores objects that are larger than X (where X is a number in bytes).
  • -mt MethodTable – List only those objects with the MethodTable given.
  • -type TYPE – List only those objects whose type name is a substring math of TYPE.
  • -cache – Saves the objects in an internal cache for later use (helps to speed up things instead of rescanning the heap all over again).
  • -l X – Prints out only X items from each heap instead of all the objects.
  • -short – Prints out only the object address. Useful for combining with the .foreach command.
  • -fix START END – Use the given START and END addresses and scan the heap only between these addresses.

NOTE: If I remember correctly, -cache, -nostring and -short are all new commands that were added in the last two versions of SOS (previously PSSCOR), the rest are available for quite a while in most versions of SOS.

-short, the wonder parameter

Just to show you how powerful the -short option is, let say that you want to print the content of all gen 2 objects that you currently have. Prior to the -short command you had to run “!dumpheap -gen 2” copy the output to notepad, parse it to leave only object addresses and only then you could either run manually !do on each address, or use .foreach with the /f command.

Today, with -short all you need to do is run the following command line:
.foreach ( obj { !dumpheap -gen 2 -short } ) { !do ${obj} }

Quite useful, isn’t it…

  • Steve Lydiatt

    Does anyone have any idea where the -gen option on !dumpheap has gone to?

    I’m using the version of SOS from c:\winnt\Microsoft.net\Framework\v2.0.50727\sos.dll

  • http://dotnetdebug.net Eran Sandler

    Unfortunately the version of SOS that shipped with .NET 2.0 has in some commands less features than its counter part, the SOS for .NET 1.1 that is being released with WinDbg.

    That’s why the -gen parameter is missing.

  • heyho

    Great info, thanks!

    I’d like to know more about how to limit the output of !do running in context of the .foreach.

    My particular situation is that I’m debugging a dump, and all the threads have names, but cdb/windbg doesn’t show them. It’s easy to find the names with !do though, but with .foreach and lots of threads that’s a bit too much information.

  • http://dotnetdebug.net Eran Sandler

    !do dumps what it has, that’s all. Perhaps you can filter the output somehow with your own extension or function, but that will require writing more code.

    I vaguely remember that there is a regex mechanism inside WinDbg which might help you extract only parts of the output that matches the expression and that might help you out. But it might be just my imagination or an extension I saw somewhere.

  • Pingback: Limit !dumpheap (windbg) output to n objects | Ask Programming & Technology