wpf - 什么样的事情可能会导致 OutOfMemoryException?
问题描述
我遇到了内存不足异常的问题,App_DispatcherUnhandledException 捕获了错误消息(一些错误消息中的 1 条):
Message: Zgłoszono wyjątek typu 'System.OutOfMemoryException'. Source: PresentationFramework
StackTrace:
w System.Windows.TreeChangeInfo.CreateParentInheritableProperties(DependencyObject d, DependencyObject parent, Boolean isAddOperation)
w System.Windows.TreeWalkHelper.InvalidateOnTreeChange(FrameworkElement fe, FrameworkContentElement fce, DependencyObject parent, Boolean isAddOperation)
w System.Windows.FrameworkElement.OnVisualParentChanged(DependencyObject oldParent)
w System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject oldParent)
w System.Windows.Media.Visual.AddVisualChild(Visual child)
w System.Windows.FrameworkElement.set_TemplateChild(UIElement value)
w System.Windows.FrameworkTemplate.WireRootObjectToParent(Object createdObject, DependencyObject rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
w System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
w System.Windows.FrameworkTemplate.<>c__DisplayClass45_0.<LoadOptimizedTemplateContent>b__2(Object sender, XamlObjectEventArgs args)
w System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value)
w System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
w System.Xaml.XamlObjectWriter.WriteEndObject()
w System.Xaml.XamlWriter.WriteNode(XamlReader reader)
w System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
w System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
w System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
w System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
w System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
w System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
w System.Windows.FrameworkElement.ApplyTemplate()
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Control.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureCell(IColumnElement column, GridViewCellBase cell, Size size)
w Telerik.Windows.Controls.GridView.GridViewCellsPanel.RealizeAndMeasureCells(Int32 startIndex, Int32 predictedLastIndex, Func`2 calculateLastIndex)
w Telerik.Windows.Controls.GridView.GridViewCellsPanel.UpdateVirtualizedCells()
w Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureOverride(Size availableSize)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
w System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Control.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Border.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Control.MeasureOverride(Size constraint)
w Telerik.Windows.Controls.GridView.GridViewRowItem.MeasureOverride(Size availableSize)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.FlatLayoutStrategy.RealizeRows(Int32 startIndex, Int32 endIndex, Double& verticalOffset, HashSet`1& realizedRows)
w Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.FlatLayoutStrategy.MeasureOverride(Size availableSize)
w Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size availableSize)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
w System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
w Telerik.Windows.Controls.GridView.GridViewScrollViewer.MeasureOverride(Size availableSize)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Control.MeasureOverride(Size constraint)
w Telerik.Windows.Controls.GridView.GridViewDataControl.MeasureOverride(Size availableSize)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
w System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
w System.Windows.Controls.Grid.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
w System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
w System.Windows.FrameworkElement.MeasureCore(Size availableSize)
w System.Windows.UIElement.Measure(Size availableSize)
w System.Windows.ContextLayoutManager.UpdateLayout()
w System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
w System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
w System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
w System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
w System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
w System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
w System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
HResult: -2147024882
以下是一些信息:
应用程序在 RDS 上运行,有 15-25 个用户使用应用程序
可用内存:32 GB
平均使用内存:17 GB
当使用的内存达到 30 GB(最高峰值约为 23 GB)时,永远不会出现这种情况
CPU 很好,平均 40%,峰值使用 60%
每个应用程序实例的 RAM 使用量随着使用量的增长而增长,每个实例大约 1.5 GB。
应用程序被多次分析,有 0 个内存泄漏,根据 VS Profiler 和其他分析器(包括 JetBrains 之一)
应用使用 Telerik 库
应用使用实体框架
我被困住了,不知道在哪里寻找潜在问题。优化花了我数周时间,而且进展顺利——我设法修复了典型的内存泄漏和内存分配问题,有时应用程序需要手动启动 GC.Collect(),但这种情况很少发生。也没有模式,当 OutOfMemoryException 命中时,我不知道在哪里寻找问题。
最后一件事,在我看来,由于经典的 WPF 内存增长(应用程序在几个小时内从 60K 到 1.5k 的 ram 使用量),该应用程序对于 WPF 来说太大了,但我别无选择,只能解决这个问题。
编辑
我不是 RDS 环境的管理员,有另一位工程师在做部署和管理,也许他可能做了一些改变,开始导致这个问题?
解决方案
很难找出这样的确切原因,但我可以建议一些故障排除机制。
您的堆栈跟踪表明您正在使用 Telerik gridview。尝试将其注释掉或用普通的 gridview 替换,看看异常是否仍然出现。这样至少你可以确定哪个元素是罪魁祸首。一旦您确认是哪个元素导致了问题,请尝试通过从相同类型继承来创建自定义控件。然后在所有方法中添加日志调用基本方法。这样您就不会失去功能,同时可以确认出了什么问题。
检查您是否遇到某种无限循环。调用堆栈表明,当它尝试渲染所有元素时,内存不足。如果涉及某种太大的 ItemSource 或使用了循环相关控件,则可能会出现这种情况。
推荐阅读
- jquery - 如何将数据表中 columnDefs 的目标定义为“最后一个”?
- javascript - InversifyJS - 注入服务来表达中间件
- django - Django不对称关系:返回相交
- javascript - 创建一个包含多个对象的对象并将它们推送到 Javascript 中的数组
- javascript - React google place 自动完成不提供 photo_ref
- r - 创建一个计算公式的列,其中的数字来自例如 row[a] + column[a]
- c++ - 最简单的并发 C++11 字符串键控映射
- java - JAVAFX 如何使 Vbox 在 BorderPane 的右侧居中
- mysql - 如何创建此正确连接
- reactjs - 最新的 Babel (@babel/core v7) 错误“未定义导出”