【问题标题】:Aforge Memory UsageAforge 内存使用情况
【发布时间】:2011-11-27 00:03:41
【问题描述】:

我已经编写了一些需要处理大约 3000 张图像的 c# 代码,但是到第 500 张图像时,任务管理器正在显示使用 1.5gb 内存的程序。下面的函数似乎是罪魁祸首之一。我可以在这里做得更好吗?任何帮助或建议表示赞赏。谢谢。

   private void FixImage(ref Bitmap field)
    {
        //rotate 45 degrees
        RotateBilinear rot = new RotateBilinear(45);
        field = rot.Apply(field);               //Memory spikes 2mb here
        //crop out unwanted image space
        Crop crop = new Crop(new Rectangle(cropStartX, cropStartY, finalWidth, finalHeight));
        field = crop.Apply(field);              //Memory spikes 2mb here
        //correct background
        for (int i = 0; i < field.Width; i++)
        {
            for (int j = 0; j < field.Height; j++)
            {
                if (field.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
                    field.SetPixel(i, j, Color.White);
            }
        }                                  //Memory usuage increases 0.5mb by the end
    }

【问题讨论】:

  • 有没有办法通过调用GC从内存中删除这些对象?

标签: c# memory-management memory-leaks aforge


【解决方案1】:

这样更改代码时可以减少内存

private void FixImage(ref Bitmap field)
{
    //rotate 45 degrees
    RotateBilinear rot = new RotateBilinear(45);
    var rotField = rot.Apply(field);               //Memory spikes 2mb here
    field.Dispose();
    //crop out unwanted image space
    Crop crop = new Crop(new Rectangle(cropStartX, cropStartY, finalWidth, finalHeight));
    var cropField = crop.Apply(rotField);              //Memory spikes 2mb here
    rotField.Dispose();
    //correct background
    for (int i = 0; i < cropField.Width; i++)
    {
        for (int j = 0; j < cropField.Height; j++)
        {
            if (cropField.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
                cropField.SetPixel(i, j, Color.White);
        }
    }                                  //Memory usuage increases 0.5mb by the end
    field = cropField;
}

因此,立即释放该图像内存,而不是等到 GC 最终处理它,这似乎是一个好主意。

【讨论】:

  • 我明确地调用了 GC,但它也没有多大帮助。但我会试一试您更改过的代码并调用 GC 看看它有多大帮助。谢谢。
  • 内存使用此代码保持在 50K。否则每次迭代都会很快变高。图像分辨率为 3456x2304 (358KB)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 2012-05-30
  • 2021-02-26
  • 2010-10-24
  • 2015-06-14
  • 2015-01-06
  • 1970-01-01
相关资源
最近更新 更多