Analyzing IIS C# Memory Leaks

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.

  1. Open up the Task Manager
  2. Click on the Details tab
  3. Right click on any of the existing column headers (name/PID/status)
  4. Click on “Select Columns”
  5. Check the “Command Line” column and hit OK
  6. Sort the list of processes by Name
  7. Find all the w3wp.exe processes
  8. Identify the one running your app pool by looking at the “Command line” OR “User name” column
  9. Right click on the process
  10. 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:

  1. Take the dump file generated in Step 1 and place it in the machine that has VS installed.
  2. Open Visual Studio
  3. Go to File>Open>File
  4. Open the Dump File
  5. This will open the Minidump File Summary
  6. On the Actions section of the report hit “Debug Managed Memory”
  7. This opens the Managed Memory report
  8. Sort the report by “Inclusive Size” descending
  9. 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s