MVC Web应用程序中的内存泄漏

时间:2012-12-09 21:32:02

标签: asp.net-mvc out-of-memory

我在构建的MVC 3应用程序中有内存泄漏。我使用DebugDiag来获取一些调试信息,但我很难理解结果。结果指向clr!EEHeapAlloc + cb,每次页面刷新时,内存使用量都会不断上升。任何帮助将不胜感激。结果如下:

功能详情

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 

2 个答案:

答案 0 :(得分:0)

您的应用程序是否使用COM / Interops?如果是这样,那么CLR可能导致通过这些非托管对象泄漏。

答案 1 :(得分:0)

如果没有看到MVC代码,很难确切地说出造成问题的原因。 .NET的堆分配与C中的标准堆不同。由于内存基于对象具有的引用数来管理,因此.NET框架在大多数情况下处理管理此内存。但要记住的一件事是,您没有无限量的内存,这对托管运行时来说是一个很大的误解。

基本上,这里发生的是您的应用程序正在尝试为托管堆上的对象分配内存。堆被赋予一定的内存分配,当该分配中没有更多内存时,将发生垃圾收集。在垃圾收集期间,托管运行时将构建仍引用的项的图。仍然活着的物品#34;并在使用中。然后垃圾收集器将通过并移动仍然在彼此附近使用的项目。它也将在每次传递时分配一代,其背后的原因是.NET假定已存活的东西将保持活跃状态​​。然后,.NET将释放堆上不再引用的项目。如果在此之后仍然没有空间来分配内存,那么您将尝试分配OutOfMemoryException。

现在很可能在您的应用程序中发生的事情是您没有正确释放内存。有几个地方可以看。首先看一下你是否要将大量数据加载到字符串中,然后再看看你是否过度使用随时间增长的静态数据结构。每个进程都会创建一次静态数据结构,因此,如果您不断向其中插入项目而不减小其大小,则可以快速创建OutOfMemoryExceptoin。看看你是否在你的应用程序中的任何地方使用了非托管或不安全的代码,这将包括引用COM interops,这些肯定会泄漏内存,如果你使用它们,你应该使用IDisposable模式中的Marshall.Release { {3}}这将确保.NET框架在发生垃圾收集时释放COM。最后看看你是否可以实现IDisposable并使用&#34;使用&#34;语句子对象确实实现了IDisposable模式以正确处理它们。