# .NET Dump Cheat Sheet

This post collects some useful commands diagnosing memory dumps. This document is written with a sample apps and tools targeting .NET 9. Some tools still name *regions* as *segments*.

## dotnet-dump

Use [dotnet-dump](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump) to collect a memory dump.
Install using `dotnet tool install dotnet-dump -g`

It can do full dump/mini dump, etc.

> `dotnet-gcdump` while really similar creates a dumps that contains only the count and size of objects per class and the references between types to the roots.

With `dotnet-dump collect` a *Heap* dump is usually the best choice which (it does not contain 'images' (the DLLs) that is only in the full dump).

For example, collect Heap dump for process ID 420 that is running with elevated privileges:

```bash
sudo -E DOTNET_ROOT=/home/Home/.dotnet /home/Home/.dotnet/tools/dotnet-dump collect --type Heap -p 418
```

Notice that the *-E* exports the DOTNET_ROOT environment variable.

Use `dotnet-dump analyze` to analyze the dump. This offers the same SOS commands for the analysis as one might have used with WinDBG.

For example, let's find the address of a C# string literal `Hello` in the dump. the string literal is used in an anonymous method.

I am going to analyze the previously save dump file

```bash
dotnet-dump analyze core_<date>_<hours>
```

Find the anonymous method

- name2ee (return a MT entry for a given type name)
- eeheap (displays the heaps/regions of the app)
- gcwhere <address> (displays the heap/region informtion of an object)
- dumpheap (display all objects and types allocated with memory addresses)
- gcroot <address> (display the reference path to roots)
- threads (display threads)
- syncblk (for analyzing locks)
- dumpmt (display method table)
- dumpmd (display method descriptor)
- d (read memory)
- eestack (stacks)

## Others useful tips:

Add .NET and .NET tools to the path, for example:

```bash
nano ~/.bashrc
...
export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$HOME/.dotnet:$HOME/.dotnet/tools
```