BitmapPalette内存不足异常 [英] BitmapPalette Insufficient memory exception

查看:126
本文介绍了BitmapPalette内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段循环运行的代码.

I have the following piece of code that runs in a loop.

public void Test(Bitmap bmp)
{
   FormatConvertedBitmap fBitmapSource = new FormatConvertedBitmap();
   PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
   BitmapImage bi = new BitmapImage();

   using (MemoryStream ms = new MemoryStream())
   {
      bmp.Save(ms, ImageFormat.Png);
      bmp.Dispose();
      bmp = null;

      bi.BeginInit();
      bi.StreamSource = ms;
      bi.EndInit();

      BitmapPalette pallete = new BitmapPalette(bi, 256);
      ...

最后一行

BitmapPalette pallete = new BitmapPalette(bi, 256);

有时会引发以下异常

Insufficient memory to continue the execution of the program.at System.Windows.Media.Imaging.BitmapPalette..ctor(BitmapSource bitmapSource, Int32 maxColorCount)

有什么想法吗?我显然有足够的内存来继续执行.

Any ideas ? I clearly have enough memory to continue execution.

推荐答案

托管程序中还有其他OutOfMemoryException来源,与耗尽托管内存没有任何关系.转换旧有本机代码返回的错误代码时,也会引发该异常.类似于COM方法调用可以返回的E_OUTOFMEMORY错误.与您的情况相关,由GDI +提供.它只有20个不同的错误代码指示失败,您可以在

There are other sources of OutOfMemoryException in a managed program that don't have anything to do with running out of managed memory. The exception is also raised when it translates error codes returned by legacy native code. Like the E_OUTOFMEMORY error that can be returned by COM method calls. And relevant in your case, by GDI+. Which has only 20 distinct error codes to indicate failure, you'll find them documented in this answer. One of them is OutOfMemory.

这可能意味着不止一件事.用光了不受管理的内存,GDI +用来存储位图像素的那种内存肯定是可能的.这也可能意味着您的进程用尽了可用的GDI对象句柄,Windows施加了10,000个GDI句柄的句柄配额. btw是一个巨大的数字,超过该配额几乎总是表明代码中存在错误.手柄泄漏.对于托管程序,这几乎总是由忘记使用Image.Dispose()方法而导致的垃圾收集器运行得不够频繁而导致终结器释放句柄引起的.

Which can mean more than one thing. Running out of unmanaged memory, the kind used by GDI+ to store bitmap pixels is certainly a possibility. It can also mean that your process has run out of available GDI object handles, Windows imposes a handle quota of 10,000 GDI handles. Which is an enormous number btw, exceeding that quota almost always indicates a bug in the code. A handle leak. Which in the case of a managed program is almost always caused by forgetting to use the Image.Dispose() method and not having the garbage collector run often enough to allow the finalizer to release handles.

可悲的是,它甚至可能由损坏的位图数据触发,在您的情况下不太可能触发,因为您在分配调色板时大吃一惊.这表示手柄泄漏,应该在Taskmgr.exe的进程"选项卡中很容易看到.查看+选择列,然后勾选GDI对象.在测试过程时,请注意过程的显示值.稳定增长的数字意味着麻烦,当节目达到10,000时,节目就结束了.另外,请查看提交大小"列,该列可能会显示您在使用过多的非托管内存时遇到了麻烦.

Sadly it can even be triggered by corrupted bitmap data, not likely in your case since you bomb on allocating the palette. Which indicates a handle leak, which ought to be readily visible in Taskmgr.exe, Processes tab. View + Select columns and tick GDI Objects. Keep an eye on the displayed value for your process while you test it. A steadily increasing number spells trouble, the show is over when it reaches 10,000. Also look at the "Commit size" column, that can show you trouble with consuming too much unmanaged memory.

这篇关于BitmapPalette内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆