内存不足异常C#freezable.freeze [英] Out of memory exception C# freezable.freeze

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

问题描述

我正在尝试将一些图片复制到RAM,但这会导致内存不足异常. 我不知道为什么,但是我认为这是"freeze()"的原因.但是如何解冻",这真的是问题吗?

I'm trying to copy some pictures to RAM but this leads to a Out of Memory Exception.. I don't know why but I think it's cause of the "freeze()". But how to "unfreeze" and is this really the problem?

        public void preLoadThread(Object o)
    {
        Overlay ov = (Overlay)o;
        ImageSource tempNext = BitmapConverter(ov.tempPreLoadPathNext);
        ImageSource tempPrev = BitmapConverter(ov.tempPreLoadPathPrev);
        tempNext.Freeze();
        tempPrev.Freeze();
        ov.Dispatcher.Invoke(
            DispatcherPriority.Normal,
            (Action)delegate()
            {
                ov.preLoadedNext = tempNext;
                ov.preLoadedPrev = tempPrev;
                ov.preLoadPathNext = ov.tempPreLoadPathNext;
                ov.preLoadPathPrev = ov.tempPreLoadPathPrev;
            }
        );
    }

    public BitmapSource BitmapConverter(String path)
    {
        System.Drawing.Bitmap b = null;
        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
        {
            try
            {
                b = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(fs);
            }
            catch (Exception)
            {
                GC.Collect();
                GC.WaitForFullGCComplete();
            }
            fs.Close();
        }

        if ( b == null)
        {
            // Error
            return null;
        }

        BitmapSizeOptions options = BitmapSizeOptions.FromEmptyOptions();
        BitmapSource bs = null;
        try
        {
            bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
               b.GetHbitmap(),
               IntPtr.Zero,
               Int32Rect.Empty,
               options);
        }
        catch (Exception)
        {
            GC.Collect();
            GC.WaitForFullGCComplete();
        }
        return bs;
    }

推荐答案

我衷心地怀疑内存异常是否来自Freeze()调用,因为它确实没有分配任何内存.

I sincerely doubt the memory exception is coming from the Freeze() call, as that really isn't allocating any memory.

我非常确定您有GDI泄漏...在调用CreateBitmapSourceFromHBitmap()之后,必须在创建的位图上调用DeleteObject,但是由于要调用GetHbitmap()作为参数,因此无法删除.

I'm pretty sure you have a GDI leak... You have to call DeleteObject on the bitmap you create after you call CreateBitmapSourceFromHBitmap()... but because you're calling GetHbitmap() as a parameter, you have no handle to delete.

尝试一下:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

...

IntPtr hObject = b.GetHbitmap();
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
           hObject,
           IntPtr.Zero,
           Int32Rect.Empty,
           options);

DeleteObject(hObject);


Henk是对的,您不应该强制执行GC收集……它并没有真正地帮助您,因为无论如何您实际上都没有释放要收集的任何东西(要释放的唯一东西必须通过DeleteObject清理. ().


Henk is right, you shouldn't be forcing a GC collection... it's not really helping you, because you're not really freeing anything to be collected, anyway (the only thing you're freeing has to be cleaned up via DeleteObject().

我们正在谈论多少张1378x2000图片?即使您修复了GDI泄漏,这些都是大问题,并且会很快耗尽内存.

How many 1378x2000 images are we talking about? Even if you fix your GDI leak, those are big pictures and will eat up memory quickly.

Curtisk是正确的,您不能取消冻结,必须克隆...但是这样做时您将分配内存.只是警告您.

Curtisk is right, you can't unfreeze, you have to clone... but you'll be allocating memory when you do so. Just to warn you.

我认为不能在64位上运行...

I suppose running under 64-bit isn't an option...

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

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