How can I programmatically recycle a .net web app's own apppool?

前端 未结 4 1717
故里飘歌
故里飘歌 2021-01-04 11:00

I have a complex server application that uses Nhibernate and Linq2SQL. About 3 times per day the Linq2sql code generates a \"value cannot be null\" exception. Once this happ

4条回答
  •  梦毁少年i
    2021-01-04 11:14

    The solution I eventually chose was to set a maximum value for the amount of memory the appPool was allowed to consume. The code then simply gobbled memory until asp.net decided to recycle the appPool.

    In the App pool advanced settings I set the Private Memory Limit to 800,000 Kb.

    In the Catch section where the Linq code failed, I allocate more memory than the limit:

    List listOfMemory = new List();
    
    // in the app pool, you need to set the virtual memory limit to 800,000kb
    log.Error("Allocating so much memory that the app pool will be forced to recycle... ");
    for (int intCount = 1; intCount < 10000000; intCount++)
    {
    listOfMemory.Add("new string " + intCount.ToString());
    }
    

    This now means only about 4 threads fail before a new w3wp process is spawned. Prior to this solution, threads would consistently fail until a human recycled the app pool manually. Unfortunately, if you set the app pool to recycle on a regular number of minutes, the smaller the number of minutes the more often the crash. And the larger the number of minutes, the more threads would fail.

    This creative workaround limits the damage.

提交回复
热议问题