C# 位图 - 找不到如何删除 OutOfMemoryException

标签 c# memory-leaks out-of-memory

如果我执行下面的代码,OutOfMemoryException 发生在任何一行

using (Bitmap bitmap1 = new Bitmap(FrameToFilePath(interval.Start - 1)))

或行

using (Bitmap bitmap2 = new Bitmap(FrameToFilePath(interval.End + 1)))

内部 for 语句执行了大约 1000 次时。

但是,我不知道为什么会出现OutOfMemoryException。我想我已经写了足够多的 using 来处理 Bitmap 对象。内存泄漏发生在什么地方?

class Program
{
    static void Main(string[] args)
    {
        // Some code to initialize List<Interval> intervals

        Parallel.ForEach(intervals, interval =>
        {
            using (Bitmap bitmap1 = new Bitmap(FrameToFilePath(interval.Start - 1)))
            using (Bitmap bitmap2 = new Bitmap(FrameToFilePath(interval.End + 1)))
            {
                for (int i = interval.Start; i <= interval.End; i++)
                {
                    ColorMatrix colorMatrix = new ColorMatrix(); // Identity matrix
                    colorMatrix.Matrix33 = (i - interval.Start + 1F) / (interval.Span + 1F); // Alpha

                    using (ImageAttributes imageAttributes = new ImageAttributes())
                    using (Bitmap intermediate = new Bitmap(bitmap1.Width, bitmap1.Height, PixelFormat.Format32bppArgb))
                    using (Graphics graphics = Graphics.FromImage(intermediate))
                    {
                        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                        graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;

                        graphics.DrawImage(bitmap1, 0, 0);
                        graphics.DrawImage(bitmap2, new Rectangle(0, 0, intermediate.Width, intermediate.Height), 0, 0, bitmap2.Width, bitmap2.Height, GraphicsUnit.Pixel, imageAttributes);

                        intermediate.Save(FrameToFilePath(i), ImageFormat.Png);
                    }
                }
            }
        });
    }

    static string FrameToFilePath(int frame)
    {
        return string.Format(@"C:\(some path)\frames\frame-{0:00000}.png", frame);
    }
}

class Interval
{
    public int Start { get; set; }
    public int End { get; set; }
    public int Span { get { return End - Start + 1; } }
}

编辑:好的。可能是因为 Parallel.ForEach 在其他任务结束之前生成了任务,所以越来越多的任务开始了,但是 Bitmap 没有被 GC,因为任务没有结束。那么,如果是这种情况,如何解决它以避免 OutofMemoryException?我不确定是否是这种情况......

最佳答案

我遇到了类似的(但不够相似,我不想将其标记为重复)issue myself .您可以做的是将 ParallelOptions.MaximumDegreeOfParallelism 设置为在发现工作停滞时不创建额外的任务。

Parallel.ForEach(intervals,
    new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
    (interval) =>
        {
           //...
        });

您遇到此问题的原因是,在读取从 FrameToFilePath 返回的文件期间,您受约束的不是 CPU,而是驱动器的 IO。这会导致 CPU 百分比较低,Parallel.ForEach 检测到该百分比较低并尝试生成更多任务以使使用率达到 100%。设置最大并行度会告诉它在创建一定数量的任务后停止尝试达到 100%。您可以使用大于 Environment.ProcessorCount 的数字,但您的主要瓶颈是您的文件 IO。

关于C# 位图 - 找不到如何删除 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767155/

相关文章:

c# - 不在 LINQ 中?

java - org.hibernate.impl.SessionFactoryImpl 对象的正常大小是多少?

c - 动态数组和结构 C 的 Valgrind 内存泄漏

scala - 大 map 的不可变 map 实现

c# - 调用泛型类构造函数的困境

c# - 3d XNA/WP7 游戏工具和技术的良好组合

ios - 使用 UITableView dequeueReusableCell 时出现内存泄漏

go program out of memory ec2资源太小?

java - java应用程序中无限循环的内存不足和stackoverflow异常

c# - WPF DataGrid 列 TextWrapping 以编程方式