How to collect memory dumps using Managed GC Aware Dump


Introduction

Process memory dumps are very efficient sources of information that are used for troubleshooting websites, which is the reason they are frequently requested by Sitecore Support.

Various tools exist that allow collecting the memory dumps of a specific process, both based on a user request (on-demand), as well as based on a specific event (conditional).

This article describes the way to gather memory dumps using the Managed GC Aware Dump application.

For other methods of gathering memory dumps, refer to the following article:

Tool overview and download link

The Managed GC Aware Dump tool is a free command line tool that allows collecting memory dump files on-demand or right before garbage collection (GC) of a particular heap generation.

The primary difference between this tool and other memory dump collection techniques is allowing to collect dump files considering the timing of garbage collection operations. This guarantees that .NET memory heap in the produced file can always be analyzed without issues.

The tool can be downloaded from ManagedGcAwareDump.zip.

To learn more about Garbage Collection in .NET, refer to the following articles:

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals

Gathering memory dump files on-demand

To collect a memory dump of the process at a custom point of time, run the Managed GC Aware Dump tool as follows:

ManagedGcAwareDump_x64.exe [Name or PID]

Note:
You must select the appropriate version of the tool (ManagedGcAwareDump_x64.exe or ManagedGcAwareDump_x86.exe) depending on the bitness of the target process.

Note:
The tool requires a process name or process ID to be passed instead of the "[Name or PID]" argument to identify the target process.

Use the process name (such as w3wp.exe for IIS 6 or later) when there is only one process with this name.

Specify the process ID when there is more than one process with the w3wp.exe name. For information on how to get the Process ID of a particular Sitecore instance, refer to the following article: How to identify process ID of appropriate Sitecore instance.

Gathering memory dump files on-demand with GC

Sometimes it is useful to trigger the full garbage collection before gathering the memory dump file. This simplifies the process's memory heap analysis because only live (rooted) objects are present. That is especially useful during high memory usage investigations.

1. Trigger full GC and collect a memory dump file.

[Automatic mode]

You can use the Managed GC Aware Dump tool to automatically trigger GC and collect a memory dump file right after GC has been completed. To do that, run the tool with the "/fgc" parameter.

ManagedGcAwareDump_x64.exe /fgc [Name or PID]
ManagedGcAwareDump_x86.exe /fgc [Name or PID]

Note:
This feature has specific requirements. It can be used only if the following conditions are met:


[Manual mode]

If your application uses an earlier version of .NET or Visual C++ Redistributable package is not installed, you must perform this operation manually, as follows:

  1. Download and unzip the GCTrigger.zip archive.
  2. Copy the "GCTrigger.aspx" file to the "/Website" folder.
  3. Configure the ProcDump tool to gather the dump file on a specific exception. Use the "Collect a memory dump file based on a specific first chance exception scenario"(KB0253710#specificfirstchance) and specify the OnGCEndException exception type.
  4. Open the page from point 2 in the web browser (http://hostName/GCTrigger.aspx), and click Force GC.

After GC has been finished, the ProcDump tool should collect the memory dump file.

2. Trigger full GC, wait for pending finalizers, and collect memory dump file.

Use the same steps as for scenario 1, however, apply the following adjustments.

[Automatic mode] 

Run the tool with the "/fgc /wpf" parameters:

ManagedGcAwareDump_x64.exe /fgc /wpf [Name or PID]
ManagedGcAwareDump_x86.exe /fgc /wpf [Name or PID]

[Manual mode]

Click Force GC and wait for pending finalizers on the page.

Gathering memory dump files on GEN collection

In particular scenarios, it might be useful to collect one or several memory dump files prior to the GC of a specific heap generation. This might be useful during the investigation of frequent GCs or high GC pressure.

Note: It is possible to use this feature only if your application uses .NET 4.0 or later.

1. Gather a single memory dump file before GC of a specific heap generation.

To gather a memory dump file before specific generation collection, run the tool with the "/g[Gen]" parameter, where [Gen] - generation number (0,1,2).

ManagedGcAwareDump_x64.exe /g[Gen] [Name or PID]
ManagedGcAwareDump_x86.exe /g[Gen] [Name or PID]

2. Gather multiple memory dump files before GC of a specific heap generation.

To gather multiple memory dump files before the specific generation collection (one memory dump per GC), run the tool with the "/g[Gen]:[N], where [Gen] - generation number (0,1,2), N - number of memory dumps to collect.

ManagedGcAwareDump_x64.exe /g[Gen]:[N] [Name or PID]
ManagedGcAwareDump_x86.exe /g[Gen]:[N] [Name or PID]

Example. Collect 3 memory dump files before the generation 2 collections:

ManagedGcAwareDump_x64.exe /g2:3 [Name or PID]