Question

I have the following method which presents IIS worker requests on a pretty webpage however every time the page is refreshed it increases the memory consumption of dllhost (COM Surrogate) on the target machine until there is no more memory available. I'm a beginner when it comes to C# so I'm hoping somebody can explain to me how I can change this code to release the memory footprint on the target server each time its refreshed?

protected void Page_Load(object sender, EventArgs e)
{

    manager = ServerManager.OpenRemote("Lonappu01032");

    int filtered = Convert.ToInt32(Request.QueryString["filter"]);
    StringBuilder sb = new StringBuilder();

    foreach (WorkerProcess proc in manager.WorkerProcesses)
    {
        RequestCollection rc = proc.GetRequests(0);

        var selected = from r in rc
                       select r;
         if (filtered>0)
         {
             selected  = from r in rc
                                  where r.SiteId == filtered
                                  select r;

         }


        foreach (Request r in selected)
        {

            sb.AppendFormat("<tr><td><a href=\"?filter={8}\">{8}</a></td><td>{0}</td><td>{1}</td><td>{2}</td><td><img src=\"http://mobilust.net/onlines/iptoflag.aspx?ip={3}\" alt=\"{3}\" /> {3}</td><td>{4} ({5}s)</td><td>{6}</td><td>{7}</td></tr>", r.HostName, Server.HtmlEncode(r.Url), r.Verb, r.ClientIPAddr, r.PipelineState, TimeSpan.FromMilliseconds(r.TimeInState).TotalSeconds, r.CurrentModule, TimeSpan.FromMilliseconds(r.TimeElapsed).TotalSeconds, r.SiteId);
            RequestCount++;
        }

    }
}
Was it helpful?

Solution

First part (a general good practice in C#)

In .NET use the 'using' clause when working with a class that implements IDisposable. Doing this ensures the dispose method will get called when you're done. It just so happens that the ServerManager class implements IDisposable, how nice. By itself that 'should' be enough.

So implement 'using' like below and it might take care of your issue:

protected void Page_Load(object sender, EventArgs e)
{

    using (var manager = ServerManager.OpenRemote("Lonappu01032"))
    {
        int filtered = Convert.ToInt32(Request.QueryString["filter"]);
        StringBuilder sb = new StringBuilder();

        foreach (WorkerProcess proc in manager.WorkerProcesses)
        {
            //your foreach remains unchanged
        }
    }
}

Second part (there's always a but)

However it appears there is a known bug that may not be fixed depending on which version of windows / IIS you're using. see here for example use and reported bug: https://connect.microsoft.com/VisualStudio/feedback/details/722272/microsoft-web-administration-servermanager-memory-leak

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top