我已閱讀this,this,this和this加上一打其他職位/博客。Application_End應該觸發一個自動App Pool Recycle嗎?
我在經常回收的共享主機中有一個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");
}
我們的日誌文件中充斥着「應用程序啓動」項下面的代碼,但既不Application_OnEnd
,Application_End
,也不Application_Error
是不斷在這些自發的重新發射。我知道他們正在工作,因爲有觸摸web.config或/ bin文件的條目。我們還運行了內存過載測試,並且可以觸發中的OutOfMemoryException
。
我們試圖確定虛擬內存限制是否導致回收。我們在整個代碼中都添加了GC.GetTotalMemory(false)
,但是這是針對的所有.Net,而不僅僅是我們的應用程序池,對不對?我們也試過
var oPerfCounter = new PerformanceCounter();
oPerfCounter.CategoryName = "Process";
oPerfCounter.CounterName = "Virtual Bytes";
oPerfCounter.InstanceName = "iisExpress";
logger.Debug("Virtual Bytes: " + oPerfCounter.RawValue + " bytes");
但是沒有共享主機的權限。
我監測與用ANTS內存分析器連接,似乎無法找到元兇的再循環生產同一請求dev的服務器上的應用程序。我們還使用dev中附帶的調試器來運行它,以檢查可能導致應用程序中止的衍生線程中的未捕獲異常。
我的問題是:
- 我怎樣纔能有效地監控共享主機告訴我的應用程序是多少之前,應用循環消耗內存使用情況?
- 爲什麼不調用global.asax中的Application_ [End/OnEnd/Error]處理程序?
- 我該怎樣才能確定是什麼導致這些回收?
謝謝。
編輯:由@JaniHyytiäinen基於答案
場景: 線程#1開始,其次是線#2。線程#1命中內存限制但繼續處理。線程#3開始。線程#1完成,但#1在#1達到內存限制後超過60秒。
游泳池然後不正常地中止?什麼http響應將#2 &#3收到(這些是AJAX調用,但我在Fiddler中得到504錯誤)?
接受#3的請求還是隻是排隊直到新的池啓動?
有什麼辦法可以知道內存限制已經被觸發或將要被觸發嗎?
任何策略的歡迎。
您的網站實際上並未返回HTTP 504網關超時,因爲錯誤本身意味着url請求的資源沒有及時響應。在你的情況下,可能是因爲應用程序池被關閉了,新的還沒有啓動。當應用程序池關閉時,它中的所有線程都會中止。這絕對會導致線程#2和#3上的HTTP 504 –