So, you have an application that uses a lot of threads and you would like to know how to trigger a dump

when the number of threads exceeds a particular limit. Well, look no further, this is how you can do it.

 

Let’s do it by example. Start Visual Studio and create a new web site. Add a button to the default.aspx page

and set the code behind for the page to this:

 

using System.Threading;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e){}

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        List<Person> persons = new List<Person>();

        int noOfPersons = 25;

        int sleepTime = 20000;

 

        for (int i = 0; i < noOfPersons; i++)

        {

            persons.Add(new Person());

        }

 

        foreach (Person p in persons)

        {

            p.StartUpdate(sleepTime);

        }

    }

 

    class Person

    {

        public Thread UpdateThread;

        public Person()

        {

            UpdateThread = new Thread(Person.FakeUpdateToRemoteResource);

        }

 

        public void StartUpdate(int sleepTime)

        {

            UpdateThread.Start(sleepTime);

        }

 

        public static void FakeUpdateToRemoteResource(object sleepTime)

        {

            Thread.Sleep(Convert.ToInt32(sleepTime));

        }

    }

}

 

This is naturally not how you should code anything.
This is just to emulate an application that will update list of persons. Each update on a separate thread since the working with the remote resource may be slow.

So, build the above and navigate to the Default.aspx page.

Start Performance Monitor and add the counter ‘Thread Count’ for the w3wp process under the Process section.

Then hit the button and you should see something like below, 25 threads running for 20 seconds.

 

 

So now we know that we see the behavior. So, how to dump on this.

It can be done from DebugDiag (it will use the same mechanics as below), but if you do not want to install anything then you can do it like this:

 

. Create a directory called C:\ProcDump.

. Download ProcDump from here:

http://technet.microsoft.com/en-us/sysinternals/dd996900

. Unzip the ProdDump.zip in the C:\ProcDump directory.

. Start a Command Prompt as Administrator ("Run as administrator") and navigate to the C:\ProcDump\ directory.

. Find out what the PID is for the w3wp.exe process that we want to dump, for example with Task Manager and the PID column.

. Run the following from the commandprompt.

!!! Replace both occurrences of 1234 to be the PID for your w3wp.exe !!!

 

procdump -accepteula -p "\Process(w3wp_1234)\Thread Count" 50 -s 10 -ma 1234

 

. This will generate a full (-ma) dump when the # of threads for PID 1234 is > than 50 for more than 10 seconds.

-> You can change 50 in order to dump on a higher or lower number of threads.

-> You can change 10 (-s switch) in order to dump on a higher or lower threshold that must be hit before dump is written.

-> The dump will end up in the C:\ProcDump\ directory.

 

Once ProcDump is running, hit the button again and you should get an output like below and the dump file created in the C:\ProcDump directory.

 

 

Hope this helps.