There comes a day in a developer’s life when you have a memory leak. This is not too common in the C# world but it happens. When it happens it is usually in production; and it spoils your day for sure.
In this post I’ll show you how to get a memory dump of the IIS process running your website (application pool) and then I’ll show you how to open it an analyze it.
Step 1: Getting the Memory Dump
Go to the server (production, staging, dev, wherever) where the memory leak is happening.
- Open up the Task Manager
- Click on the Details tab
- Right click on any of the existing column headers (name/PID/status)
- Click on “Select Columns”
- Check the “Command Line” column and hit OK
- Sort the list of processes by Name
- Find all the w3wp.exe processes
- Identify the one running your app pool by looking at the “Command line” OR “User name” column
- Right click on the process
- Click “Create dump file”
Step 2: Analyzing the Dump File
There are several tools for analyzing Dump Files. Visual Studio (VS) has that functionality as well and chances are you already have VS installed on your machine.
To see the C# type (class name) that is using tons of memory:
- Take the dump file generated in Step 1 and place it in the machine that has VS installed.
- Open Visual Studio
- Go to File>Open>File
- Open the Dump File
- This will open the Minidump File Summary
- On the Actions section of the report hit “Debug Managed Memory”
- This opens the Managed Memory report
- Sort the report by “Inclusive Size” descending
- The types at the top of the list are the ones using up all your memory
From here you are on your own. You need to understand what those types do and why they are using all the memory. On my particular situation, my IoC container was set up to use singletons on tons of WCF client classes. I registered those types as transcients in the container; that solved my problem.