1

我构建的 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 
4

2 回答 2

0

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

于 2014-04-09T23:14:13.180 回答
0

如果没有看到 MVC 代码,就很难确切地知道是什么导致了问题。.NET 的堆分配与 C 中的标准堆不同。由于内存是根据对象具有的引用数来管理的,因此 .NET 框架在大多数情况下会处理管理此内存。不过要记住的一件事是,您没有无限量的内存,这是对托管运行时的一个很大误解。

本质上,这里发生的是您的应用程序正在尝试为托管堆上的对象分配内存。堆被分配了一定的内存,当该分配中没有更多内存时,就会发生垃圾收集。在垃圾回收期间,托管运行时将构建仍然被引用的项目的图表。仍然“活着”并正在使用的项目。然后垃圾收集器将遍历并移动仍在使用的项目彼此靠近。它还将在每次传递时分配一代,这背后的原因是 .NET 假设已经存在较长时间的事物将继续存在。.NET 然后将释放堆上不再引用的项目。如果在此之后仍然没有空间分配你的内存'

现在,您的应用程序中最有可能发生的事情是您没有正确地释放内存。有几个地方可以看。首先查看是否将大量数据加载到字符串中,然后查看是否过度使用随时间增长的静态数据结构。静态数据结构为每个进程创建一次,因此如果您不断地向它们插入项目而不减小它们的大小,它们可以快速创建 OutOfMemoryExceptoin。查看您是否在应用程序的任何地方使用非托管或不安全代码,这将包括引用 COM 互操作,这些肯定会泄漏内存,如果您确实使用它们,您应该在 IDisposable 模式中使用 Marshall.Release http:/ /msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.release(v=vs.这将确保 .NET 框架在垃圾收集发生时释放 COM。最后看看您是否可以实现 IDisposable 并使用“使用”语句子对象来实现 IDisposable 模式来正确处理它们。

于 2014-04-09T23:41:32.777 回答