2012-12-09 26 views
1

我在構建的MVC 3應用程序中有內存泄漏。我使用DebugDiag來獲取一些調試信息,但是很難理解結果。結果指向clr!EEHeapAlloc + cb,每次刷新頁面時,它都會隨着內存使用量不斷增加而增加。任何幫助將不勝感激。結果如下:MVC Web應用程序中的內存泄漏

功能的詳細內容

Function clr!EEHeapAlloc+cb 
Allocation type Heap allocation(s) 
Allocation Count 1507120 allocation(s) 
Allocation Size 1.13 GBytes 
Leak Probability 79% 

調用堆棧樣品1

Address 0x214734a0 
Allocation Time 00:05:02 since tracking started 
Allocation Size 64 Bytes 

功能源目標

clr!EEHeapAlloc+cb  ntdll!RtlAllocateHeap 
clr!EEHeapAllocInProcessHeap+5c  clr!EEHeapAlloc 
clr!operator new+2b  clr!EEHeapAllocInProcessHeap 
clr!CPropertyArray::Set+48  clr!operator new 
clr!CAssemblyName::SetProperty+109  clr!CPropertyArray::Set 
clr!FusionBind::CreateFusionName+2ee  
clr!FusionBind::LoadAssembly+9c  clr!FusionBind::CreateFusionName 
clr!AssemblySpec::FindAssemblyFile+f4  
clr!AppDomain::BindAssemblySpec+2f2  clr!AssemblySpec::FindAssemblyFile 
clr!AssemblySpec::LoadDomainAssembly+2bc  
clr!AssemblySpec::LoadAssembly+19  clr!AssemblySpec::LoadDomainAssembly 
clr!AssemblyNative::Load+297  
System.Resources.ResourceManager.InternalGetResourceSet(System.Globalization.CultureInfo, Boolean, Boolean, System.Threading.StackCrawlMark ByRef)  0x1E7B66 
System.Resources.ResourceManager.GetString(System.String, System.Globalization.CultureInfo)  
System.Environment+ResourceHelper.GetResourceStringCode(System.Object)  
clr!CallDescrWorker+33  
clr!CallDescrWorkerWithHandler+8e  clr!CallDescrWorker 
clr!MethodDesc::CallDescr+194  clr!CallDescrWorkerWithHandler 
clr!MethodDesc::CallTargetWorker+21  clr!MethodDesc::CallDescr 
clr!MethodDescCallSite::Call+1c  clr!MethodDesc::CallTargetWorker 
clr!ExecuteCodeWithGuaranteedCleanupHelper+bb  
clr!ReflectionInvocation::ExecuteCodeWithGuaranteedCleanup+138  clr!ExecuteCodeWithGuaranteedCleanupHelper 
clr!CallDescrWorker+33  
clr!CallDescrWorkerWithHandler+8e  clr!CallDescrWorker 
clr!MethodDesc::CallDescr+194  clr!CallDescrWorkerWithHandler 
clr!MethodDesc::CallTargetWorker+21  clr!MethodDesc::CallDescr 
clr!GetResourceStringFromManaged+198  
clr!GetResourceFromDefault+e2  clr!GetResourceStringFromManaged 
clr!CallDescrWorker+33  
clr!RuntimeTypeHandle::CreateInstance+61e  
clr! ?? ::FNODOBFM::`string'+42e78  
System.Collections.Generic.HashSet`1[[System.__Canon, mscorlib]].UnionWith(System.Collections.Generic.IEnumerable`1<System.__Canon>)  0x1EB0BA 
Glimpse.Mvc3.Plumbing.GlimpseDependencyResolver.GetService(System.Type)  0x86A4B2 
System_Core_ni+1cbb28  
System.Web.Mvc.ControllerTypeCache.GetControllerTypes(System.String, System.Collections.Generic.HashSet`1<System.String>)  
System.Collections.Generic.HashSet`1[[System.__Canon, mscorlib]].System.Collections.Generic.IEnumerable<T>.GetEnumerator()  
System_Core_ni+1d0f28  
System_Web_Mvc_ni+110c18  
System.Web.Mvc.DefaultControllerFactory.CreateController(System.Web.Routing.RequestContext, System.String)  
Castle.Proxies.Invocations.DefaultControllerFactory_CreateController.InvokeMethodOnTarget()  
clr! ?? ::FNODOBFM::`string'+65d52  
Castle.DynamicProxy.AbstractInvocation.Proceed()  
Castle.Proxies.DefaultControllerFactoryProxy.CreateController(System.Web.Routing.RequestContext, System.String)  Castle.DynamicProxy.AbstractInvocation.Proceed() 
System.Collections.Hashtable.InitHash(System.Object, Int32, UInt32 ByRef, UInt32 ByRef)  
System.Web.SiteMapNode.IsAccessibleToUser(System.Web.HttpContext)  
System.Web.StaticSiteMapProvider.GetChildNodes(System.Web.SiteMapNode)  
System.Web.SiteMapNode.get_ChildNodes()  
System.Web.SiteMapNode.get_HasChildNodes()  
MvcSiteMapProvider.Web.Html.MenuHelper.BuildModel(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean)  
MvcSiteMapProvider.Web.Html.MenuHelper.BuildModel(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean)  MvcSiteMapProvider.Web.Html.MenuHelper.BuildModel(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean) 
MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.String, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean)  MvcSiteMapProvider.Web.Html.MenuHelper.BuildModel(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean) 
MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean)  MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.String, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean) 
MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, Boolean, Boolean, Boolean)  MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, System.Web.SiteMapNode, Boolean, Boolean, Int32, Boolean) 
ASP._Page_Views_Shared__SubMenu_cshtml.Execute()  MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapProvider.Web.Html.MvcSiteMapHtmlHelper, Boolean, Boolean, Boolean) 
System.Web.WebPages.WebPageBase.ExecutePageHierarchy()  
0xB373A48  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase)  
System.Web.Mvc.BuildManagerCompiledView.Render(System.Web.Mvc.ViewContext, System.IO.TextWriter)  
System.Web.Mvc.Html.PartialExtensions.Partial(System.Web.Mvc.HtmlHelper, System.String, System.Object, System.Web.Mvc.ViewDataDictionary)  
ASP._Page_Views_Shared__Layout_cshtml.Execute()  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy()  
0xB3738BC  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase)  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter)  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase) 
System.Web.WebPages.WebPageBase+<>c__DisplayClass7.<RenderPageCore>b__6(System.IO.TextWriter)  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter) 
System.Web.WebPages.HelperResult.WriteTo(System.IO.TextWriter)  
System.Web.WebPages.WebPageExecutingBase.WriteTo(System.IO.TextWriter, System.Web.WebPages.HelperResult)  System.Web.WebPages.HelperResult.WriteTo(System.IO.TextWriter) 
System.Web.WebPages.WebPageBase.Write(System.Web.WebPages.HelperResult)  System.Web.WebPages.WebPageExecutingBase.WriteTo(System.IO.TextWriter, System.Web.WebPages.HelperResult) 
System.Web.WebPages.WebPageBase.RenderSurrounding(System.String, System.Action`1<System.IO.TextWriter>)  
System.Web.WebPages.WebPageBase.PopContext()  System.Web.WebPages.WebPageBase.RenderSurrounding(System.String, System.Action`1) 
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase)  System.Web.WebPages.WebPageBase.PopContext() 
System.Web.Mvc.BuildManagerCompiledView.Render(System.Web.Mvc.ViewContext, System.IO.TextWriter)  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult)  
Castle.Proxies.ControllerActionInvokerProxy.InvokeActionResult_callback(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult)  System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult) 
Castle.Proxies.Invocations.ControllerActionInvoker_InvokeActionResult.InvokeMethodOnTarget()  
Castle.DynamicProxy.AbstractInvocation.Proceed()  
Castle.Proxies.ControllerActionInvokerProxy.InvokeActionResult(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult)  Castle.DynamicProxy.AbstractInvocation.Proceed() 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1<System.Web.Mvc.ResultExecutedContext>)  
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()  System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(System.Web.Mvc.ControllerContext, System.Collections.Generic.IList`1<System.Web.Mvc.IResultFilter>, System.Web.Mvc.ActionResult)  
System.Web.Mvc.ControllerActionInvoker.InvokeAction(System.Web.Mvc.ControllerContext, System.String)  
System.Web.Mvc.ControllerBase.Execute(System.Web.Routing.RequestContext)  
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(System.Web.Routing.RequestContext)  
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass1.<MakeVoidDelegate>b__0()  
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass8`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].<BeginSynchronous>b__7(System.IAsyncResult)  
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].End()  
System_Web_Mvc_ni+49938  
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(System.Action)  
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(System.Action)  
System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult)  System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(System.Action) 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult)  
System.Web.HttpApplication.ResumeStepsFromThreadPoolThread(System.Exception)  
System.Web.HttpApplication+AsyncEventExecutionStep.OnAsyncEventCompletion(System.IAsyncResult)  System.Web.HttpApplication.ResumeStepsFromThreadPoolThread(System.Exception) 
System.Web.HttpAsyncResult.Complete(Boolean, System.Object, System.Exception, System.Web.RequestNotificationStatus)  
System.Web.SessionState.SessionStateModule.PollLockedSessionCallback(System.Object)  System.Web.HttpAsyncResult.Complete(Boolean, System.Object, System.Exception, System.Web.RequestNotificationStatus) 
0x8CFA453  
System.Threading._TimerCallback.TimerCallback_Context(System.Object)  
System.Threading.ExecutionContext.runTryCode(System.Object)  
clr!CallDescrWorker+33  
clr!CallDescrWorkerWithHandler+8e  clr!CallDescrWorker 
clr!MethodDesc::CallDescr+194  clr!CallDescrWorkerWithHandler 
clr!MethodDesc::CallTargetWorker+21  clr!MethodDesc::CallDescr 
clr!MethodDescCallSite::Call+1c  clr!MethodDesc::CallTargetWorker 
clr!ExecuteCodeWithGuaranteedCleanupHelper+bb  
clr!ReflectionInvocation::ExecuteCodeWithGuaranteedCleanup+138  clr!ExecuteCodeWithGuaranteedCleanupHelper 
clr!CallDescrWorker+33  
clr!CallDescrWorkerWithHandler+8e  clr!CallDescrWorker 
clr!DispatchCallBody+20  clr!CallDescrWorkerWithHandler 
clr!DispatchCallDebuggerWrapper+75  clr!DispatchCallBody 
clr!DispatchCallNoEH+53  clr!DispatchCallDebuggerWrapper 
clr!AddTimerCallback_Worker+70  clr!DispatchCallNoEH 
clr!Thread::DoExtraWorkForFinalizer+114  
clr! ?? ::FNODOBFM::`string'+36051  
clr!DebuggerU2MCatchHandlerFrame::`vftable'  
clr!_except_handler4  
clr! ?? ::FNODOBFM::`string'+366e2  
clr!Thread::RaiseCrossContextException+3e1  
0x1089228  
clr!`string'+3c  
clr!`string'+3c  
clr! ?? ::FNODOBFM::`string'+36051  
clr!DebuggerU2MCatchHandlerFrame::`vftable'  
clr!_except_handler4  
clr! ?? ::FNODOBFM::`string'+29219  
clr!ThreadpoolMgr::AsyncTimerCallbackCompletion+83  
clr!UnManagedPerAppDomainTPCount::DispatchWorkItem+195  
clr!ThreadpoolMgr::WorkerThreadStart  
clr!SlowClrFlsSetValue+42  
clr! ?? ::FNODOBFM::`string'+41b4f  
clr!ThreadpoolMgr::WorkerThreadStart  
clr!ThreadpoolMgr::WorkerThreadStart  
clr!operator delete+41  
clr!ThreadpoolMgr::WorkerThreadStart  
clr! ?? ::FNODOBFM::`string'+41bd8  
clr!Thread::intermediateThreadProc+4b  
kernel32!BaseThreadInitThunk+e  
ntdll!__RtlUserThreadStart+70  
ntdll!_RtlUserThreadStart+1b  ntdll!__RtlUserThreadStart 
clr!Thread::intermediateThreadProc 
+0

那麼,這將意味着CLR泄漏內存?雖然不是不可能的,但是很少見...... – rene

+0

你注意到那是一個數組創建嗎? – Aristos

+1

不知道,但網站確實使用mvcsitemap,當我刪除這個內存泄漏是少得多,但clr仍然增加,儘管非常緩慢,所以不知道如果mvcsitemap導致泄漏或其他東西,並有mvcsitemap在該網站只是增加了內存泄漏。其他使用的插件:結構圖,webactivator和實體框架4.3 – user203995

回答

0

你的應用程序使用COM/Interops嗎?如果是這樣,那麼CLR可能會導致通過這些非託管對象的泄漏。

0

沒有看到MVC代碼,很難確切地說出是什麼導致了問題。 .NET的堆分配與C中的標準堆不同。由於內存是基於對象所具有的引用數進行管理的,因此大多數情況下,.NET框架會處理管理該內存。但要記住的一點是,你沒有無限量的內存,這對於託管運行時是一個很大的誤解。

基本上這裏發生的事情是您的應用程序試圖爲託管堆上的對象分配內存。堆被給予一定的內存分配,並且當沒有更多內存時,分配垃圾回收就會發生。在垃圾收集期間,託管運行時將構建仍然引用的項目的圖形。仍「活着」且正在使用的項目。垃圾收集器然後將通過並移動彼此靠近的項目。它還會在每次傳遞時分配一代,這背後的原因是.NET假定已經存活更長的事情將保持活躍。 .NET然後將釋放堆中不再被引用的項目。如果在此之後仍然沒有剩餘空間來分配您嘗試分配的內存,則拋出OutOfMemoryException。

現在最有可能發生在你的應用程序中的是你沒有正確地釋放內存。有幾個地方可以看。首先查看是否將大量數據加載到字符串中,然後查看是否過度使用隨時間增長的靜態數據結構。每個進程創建一次靜態數據結構,因此如果您不斷向其中插入項目而不減小其大小,他們可以快速創建OutOfMemoryExceptoin。看看你是否在你的應用程序的任何地方使用了非託管或不安全的代碼,這將包括引用COM interops,這些可以肯定會泄漏內存,如果你確實使用了它們,你應該使用Marshall.Release在一個IDisposable模式中http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.release(v=vs.110).aspx這將會確保在發生垃圾回收時.NET框架釋放COM。最後看看是否可以實現IDisposable,並使用實現了IDisposable模式的「using」語句子對象來正確處理它們。