c# - 未使用所有内存/限制时出现内存不足异常

标签 c# .net memory-management memory-leaks out-of-memory

我们这里有一个问题,我们可以有一些 OutOfMemoryException .

我们将检查如何减少内存使用,但我的问题是为什么会得到它 此时 .

根据内存分析器和 Windows 任务管理器,应用程序仅重 400MB。

据我了解(confirmed here),对于 32 位应用程序,限制应该在 2GB 左右。我的电脑有 16GB 的内存,并且有很多可用的内存(超过 4GB)。

那么为什么我现在会收到此错误?

我的问题不是关于为什么我的应用程序的内存在增长,而是更多地了解为什么它现在已经发生了。
我觉得这个限制不是固定的,但我找不到任何引用。

如果有帮助,调用堆栈:

System.OutOfMemoryException: Out of memory.
   at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
   at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
   at Nevron.GraphicsCore.NBitmapGdiRenderSurface.Paint(Object sender, PaintEventArgs e, l1ll11Il1 contentPainter)
   at Nevron.Chart.WinForm.NControlView.Paint(Object sender, PaintEventArgs e)
   at Nevron.Chart.WinForm.NChartControl.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

编辑
我得到了同样的异常,但堆栈跟踪完全不同:
System.ComponentModel.Win32Exception (0x80004005): Not enough storage is available to process this command
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, Int32 ulWidth, Int32 ulHeight, IntPtr& ppvBits)
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.CreateBuffer(IntPtr src, Int32 offsetX, Int32 offsetY, Int32 width, Int32 height)
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.Allocate(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
   at DevExpress.Utils.Drawing.XtraBufferedGraphicsContext.Allocate(Graphics targetGraphics, Rectangle targetRectangle)
   at DevExpress.XtraBars.Docking2010.Views.BaseViewPainter.Draw(GraphicsCache cache, Rectangle clip)
   at DevExpress.XtraBars.Docking2010.Views.BaseView.Draw(GraphicsCache cache, Rectangle clip)
   at DevExpress.XtraBars.Docking2010.DocumentManager.PaintCore(Graphics g, Rectangle bounds)
   at DevExpress.XtraBars.Docking2010.DocumentManager.DevExpress.XtraBars.Docking2010.IDocumentsHostOwner.Paint(Graphics g)
   at DevExpress.XtraBars.Docking2010.DocumentsHost.OnPaint(Graphics g)
   at DevExpress.XtraBars.Docking2010.DocumentsHost.DoPaint(Message& m)
   at DevExpress.XtraBars.Docking2010.DocumentsHost.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

最佳答案

一种可能性是您的大型对象堆已变得碎片化。 IE。它有足够的空间,但没有足够大的空间来满足大对象的分配。

LOH 通常不会被压缩,尽管似乎有办法执行 one off compaction now .

如果这确实是问题所在,那么避免 LOH 碎片的一种方法是使用大型对象池,当您完成它们时,它们会返回到池中,而不是让 GC 处理它们。

关于c# - 未使用所有内存/限制时出现内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33122959/

相关文章:

memory-management - 如何更改 Linux 内核交换守护进程 (kswapd) 超时?

c# - 如何在 WPF 中绘制像素

具有枚举值的 C# Propertygrid 组合框(Win Forms)

c# - 通过引用传递 IDisposable 对象会导致错误?

c# - 使用 Azure CLI 在 Rider 中获取 Azure Key Vault 的访问 token

c - 释放 C 中为 char* 分配的内存

c# - 使用 Tab 键关注 DataGrid 中的 ComboBox

c# - 在 WPF 中设置和删除工具提示

c# - MEF:目录部分目录

android - 关于 dalvik 初始堆大小