Application_End应该在自动App Pool Recycle上启动吗?

Lar*_*mie 8 asp.net iis memory-management

我看了这个,这个,这个这个加上一打其他职位/博客.

我在共享主机中有一个ASP.Net应用程序,经常回收.我们使用NLog并在global.asax中包含以下代码

void Application_Start(object sender, EventArgs e) 
{
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Debug("\r\n\r\nAPPLICATION STARTING\r\n\r\n");
}
protected void Application_OnEnd(Object sender, EventArgs e)
{
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Debug("\r\n\r\nAPPLICATION_OnEnd\r\n\r\n");
}

void Application_End(object sender, EventArgs e) 
{
        HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic  | BindingFlags.Static  | BindingFlags.GetField,  null,  null,  null);

if (runtime == null)
    return;

string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",  BindingFlags.NonPublic  | BindingFlags.Instance  | BindingFlags.GetField,  null,  runtime,  null);

string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack",   BindingFlags.NonPublic  | BindingFlags.Instance  | BindingFlags.GetField,  null,  runtime,  null);

ApplicationShutdownReason shutdownReason = System.Web.Hosting.HostingEnvironment.ShutdownReason;

NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.Debug(String.Format("\r\n\r\nAPPLICATION END\r\n\r\n_shutDownReason = {2}\r\n\r\n _shutDownMessage = {0}\r\n\r\n_shutDownStack = {1}\r\n\r\n",
                shutDownMessage, shutDownStack, shutdownReason));
}

void Application_Error(object sender, EventArgs e) 
{
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Debug("\r\n\r\nApplication_Error\r\n\r\n");

}
Run Code Online (Sandbox Code Playgroud)

我们的日志文件中充斥着"应用程序启动"项,但既不是Application_OnEnd,Application_End也不Application_Error是不断在这些自发的重新发射.我知道它们正在工作,因为有触摸web.config或/ bin文件的条目.我们还运行了一个内存过载测试,可以触发一个OutOfMemoryException被捕获的内存Application_Error.

我们正在尝试确定虚拟内存限制是否导致回收.我们已经GC.GetTotalMemory(false)在整个代码中添加了,但这适用于所有.Net,而不仅仅是我们的App池,对吗?我们也试过了

var oPerfCounter = new PerformanceCounter();
oPerfCounter.CategoryName = "Process";
oPerfCounter.CounterName = "Virtual Bytes";
oPerfCounter.InstanceName = "iisExpress";
logger.Debug("Virtual Bytes: " + oPerfCounter.RawValue + " bytes");
Run Code Online (Sandbox Code Playgroud)

但是没有共享主机的权限.

我已经在一台开发服务器上监控了应用程序,该应用程序的相同请求导致了生产环境中使用ANTS Memory Profiler的回收,并且似乎无法找到罪魁祸首.我们还使用dev中附带的调试器来运行它,以检查可能导致应用程序中止的衍生线程中的未捕获异常.

我的问题是这些:

  • 如何有效监控共享主机中的内存使用情况,以便在应用程序回收之前告诉我的应用程序消耗了多少?
  • 为什么不调用global.asax中的Application_ [End/OnEnd/Error]处理程序?
  • 我怎样才能确定导致这些回收的原因?

谢谢.

编辑:根据@JaniHyytiäinen的回答

场景:线程#1开始,然后是线程#2.线程#1达到内存限制但继续处理.线程#3开始.线程#1完成,但#1在#1达到内存限制后超过60秒处理.

游泳池然后不合情理地中止?#2接收的http响应是什么(这些是AJAX调用,但我在Fiddler中遇到504错误)?
是否接受了#3的请求,或者只是在新池启动之前排队?
有没有办法知道内存限制已被命中或即将到来?

欢迎任何策略.

Jan*_*nen 15

iis中存在应用程序池关闭时间限制.假设这是60秒,并且在共享托管环境中存在应用程序池内存限制.您的应用程序池达到此限制,iis告诉应用程序池完成当前请求的所有工作.如果所有请求在60秒之前完成处理,则application_end将触发,应用程序池将正常关闭.但是,如果60秒仍然处理并且请求仍在处理中,则IIS会感到不安并杀死应用程序池.这次没有application_end会触发.同样,不会触发任何错误事件处理程序.