Iis – App Pool doesn’t respect memory limits

application-poolsiisiis-7.5kill-processprocess

I am dealing with a legacy .NET app that has a memory leak. In order to try and mitigate a run away memory situation, I've set the app pool memory limits from anywhere between 500KB to 500000KB (500MB) however the app pool doesn't seem to respect the settings as I can login and view the physical memory for it (5GB and above no matter what values). This app is killing the server and I can't seem to determine how to adjust the app pool. What settings do you recommend in order to ensure this app pool doesn't exceed around 500mb of memory.

Here is an example, the app pool is using 3.5GB of

Process List

App pool

So, the server just crashed again, and here is why:

enter image description here

The same app pool with low memory limits, a 1000 recycle request which cause a recycle event every two or three minutes but sometimes it just runs away.

I am also open to any tool that can monitor this process (either run every 30 seconds as a task or service) and can kill it when it exceeds some limit.

Best Answer

I found this post because I'm struggling to answer a similar one where limits aren't being restricted. See IIS WebLimits not being respected.

However, I can take a stab at your problem. Try the c# code below. You could do the same with powershell. You'll need to run it with admin rights.

 static void Main(string[] args)
    {

        string appPoolName = args[0];
        int memLimitMegs = Int32.Parse(args[1]);
        var regex = new System.Text.RegularExpressions.Regex(".*w3wp.exe \\-ap \"(.*?)\".*");

        //find w3wp procs....
        foreach (var p in Process.GetProcessesByName("w3wp"))
        {

            string thisAppPoolName = null;

            try
            {
                //Have to use WMI objects to get the command line params...
                using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + p.Id))
                {
                    StringBuilder commandLine = new StringBuilder();
                    foreach (ManagementObject @object in searcher.Get())
                    {
                        commandLine.Append(@object["CommandLine"] + " ");
                    }

                    //extract the app pool name from those.
                    var r = regex.Match(commandLine.ToString());
                    if (r.Success)
                    {
                        thisAppPoolName = r.Groups[1].Value;
                    }

                    if (thisAppPoolName == appPoolName)
                    {
                        //Found the one we're looking for. 
                        if (p.PrivateMemorySize64 > memLimitMegs*1024*1024)
                        {

                            //it exceeds limit, recycle it using appcmd. 

                            Process.Start(Path.Combine(System.Environment.SystemDirectory , "inetsrv", "appcmd.exe"), "recycle apppool /apppool.name:" + appPoolName);

                            Console.WriteLine("Recycled:" + appPoolName);
                        }
                    }
                }
            }
            catch (Win32Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }