【问题标题】:Simple Mandelbrot renderer runs out of memory简单的 Mandelbrot 渲染器内存不足
【发布时间】:2015-10-29 21:51:07
【问题描述】:

我一直在尝试制作一个简单的 mandelbrot 渲染器,只是为了让自己进入 c# 和表单,但是当我渲染图像时,程序有时会耗尽内存。

内存增加到 2GB,然后崩溃。

但有时它会像这样构建一个拼图模式并且不会崩溃: http://puu.sh/l2ri9/2fcd47e6d7.png

==============要渲染的代码================

Renderer.CreateGraphics().Clear(Color.White);

        double minR = System.Convert.ToDouble(MinR.Value);
        double maxR = System.Convert.ToDouble(MaxR.Value);
        double minI = System.Convert.ToDouble(MaxI.Value);
        double maxI = System.Convert.ToDouble(MinI.Value);
        int maxN = System.Convert.ToInt32(Iterations.Value);

        SolidBrush MandelColor = new SolidBrush(Color.Red);

        for (int y = 0; y < Renderer.Height; y++)
        {
            for (int x = 0; x < Renderer.Width; x++)
            {

                double cr = fitInRRange(x, Renderer.Width, minR, maxR);
                double ci = fitInIRange(y, Renderer.Height, minI, maxI);

                int n = findMandelbrot(cr, ci, maxN);

                double t = ((n + 0.0) / (maxN + 0.0));

                MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));

                Renderer.CreateGraphics().FillRectangle(MandelColor, x, y, 1, 1);

            }
        }

===链接到 Github 页面=== https://github.com/JimAlexBerger/MandelbrotProject

【问题讨论】:

  • 你是编译成 32 位还是 64 位?如果可能的话,你应该把它编译为 64 位,如果你有它,它会给你更多的内存,因为 32 位有 4GB 的内存限制。
  • 不要不要在循环中使用Renderer.CreateGraphics()。可能你不应该使用CreateGraphics 根本Renderer 是什么?一个控制?最好使用Bitmap bmp并使用using (Graphics G = Graphics.FromImage(bmp)) { your loops here!}

标签: c# graphics fractals


【解决方案1】:

为什么不将Renderer.CreateGraphics() 移出循环?内存泄漏可能是由于在Graphics 对象上没有调用IDisposable.Dispose() 而冗余调用Renderer.CreateGraphics() 引起的。

double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);

SolidBrush MandelColor = new SolidBrush(Color.Red);

using(var graphics = Renderer.CreateGraphics())
{
  graphics.Clear(Color.White);

  for (int y = 0; y < Renderer.Height; y++)
  {
      for (int x = 0; x < Renderer.Width; x++)
      {

          double cr = fitInRRange(x, Renderer.Width, minR, maxR);
          double ci = fitInIRange(y, Renderer.Height, minI, maxI);

          int n = findMandelbrot(cr, ci, maxN);

          double t = ((n + 0.0) / (maxN + 0.0));

          MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));

          gfx.FillRectangle(MandelColor, x, y, 1, 1);

      }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    相关资源
    最近更新 更多