内存泄漏在哪里

时间:2014-06-05 19:03:21

标签: c# memory-management memory-leaks out-of-memory magicknet

我在循环浏览图像时遇到内存不足异常。它发生在大约500张图像之后。

如果我尝试通过并行循环运行相同的代码,它只能通过大约5个图像。

在抛出内存异常

之前,它似乎通常会变慢

这是我的代码。我正在使用MagickNet

public static void PreProductionProcessing(DirectoryInfo dirInfo)
{
    var tw = new StreamWriter(@"C:\bar.csv", true);

    tw.WriteLine("Filename, Alpha Channel Removed, Dpi Changed, Resized");
    foreach (var currentFile in dirInfo.GetFiles("*.tif"))
    {
        var alphaChannelRemoved = false;
        var resized = false;
        var DpiChanged = false;

        Image img = null;

        MemoryStream ms = null;

        try
        {
            img = new Image(currentFile.FullName);

            if (img.Matte)
            {
                ms = RemoveAlphaChanell(currentFile.FullName);
                img = new Image(ms);
                alphaChannelRemoved = true;
            }

            if (img.Density.x_res < 300)
            {
                img.Density = new Image.Resolution(300, 300);
                DpiChanged = true;
            }

            if (img.Size.Width != 1875)
            {
                var newSize = img.Size;
                double resizeRatio = (double)newSize.Width / (double)1875;

                newSize.Width = Convert.ToInt16(img.Size.Width / resizeRatio);
                newSize.Height = Convert.ToInt16(img.Size.Height / resizeRatio);

                img.Resize(newSize);
                resized = true;
            }

            using (var filestream = new FileStream(Path.Combine(@"C:\OutFolder", currentFile.Name), FileMode.CreateNew))
            {
                img.Write(filestream, "tif");
            }

            tw.WriteLine("{0}, {1}, {2}, {3}", currentFile.Name, alphaChannelRemoved, DpiChanged, resized);

            tw.Flush();
        }
        catch (Exception e)
        {
            tw.WriteLine("{0}, {1}", currentFile.Name, e.Message);
            tw.Flush();
        }
        finally
        {
            if (img != null)
            {
                img.Dispose();
                img = null;
            }

            if (ms != null)
                ms.Dispose();

            Magick.Term();
        }
    }
}

static MemoryStream RemoveAlphaChanell(string source)
{
    Bitmap old = new Bitmap(source);
    Bitmap temp = new Bitmap(old.Width, old.Height, PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage(temp);
    g.Clear(System.Drawing.Color.White);
    var rect = new Rectangle(Point.Empty, new Size(old.Width, old.Height));
    g.DrawImage(old, rect);
    var stream = new MemoryStream();

    temp.Save(stream, ImageFormat.Tiff);
    stream.Seek(0, SeekOrigin.Begin);

    old.Dispose();
    temp.Dispose();
    g.Dispose();

    return stream;
}

我不确定内存泄漏是否在MagickNet中,或者是否有人可以在我的代码中看到导致此问题的任何内容。

任何建议都将不胜感激

1 个答案:

答案 0 :(得分:3)

img.Matte为真时,您将img变量中的图像替换为新图像。上一张图片从未被处理过。

在替换之前处理上一张图像:

        if (img.Matte)
        {
            img.Dispose();
            ms = RemoveAlphaChanell(currentFile.FullName);
            img = new Image(ms);
            alphaChannelRemoved = true;
        }