【问题标题】:ZedGraph plot: System.OutOfMemoryException thrownZedGraph 图:抛出 System.OutOfMemoryException
【发布时间】:2012-10-15 19:17:18
【问题描述】:

我已经编写或者尝试使用 ZedGraph 在 C# 中绘制可用空间薛定谔方程,并在运行创建的可执行文件时遇到 System.OutOfMemoryException 错误。

************** Exception Text **************
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Collections.Generic.List`1.set_Capacity(Int32 value)
   at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
   at System.Collections.Generic.List`1.Add(T item)
   at ZedGraph.PointPairList.Add(Double x, Double y)
   at Wavepacket.Form1.CreateGraph(ZedGraphControl schrodinger) in C:\Users\user1748005\Documents\Visual Studio 2010\Projects\Wavepacket\Wavepacket\Form1.cs:line 72
   at Wavepacket.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\user1748005\Documents\Visual Studio 2010\Projects\Wavepacket\Wavepacket\Form1.cs:line 35
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我的 Cmplx 类的必要函数/构造函数相对简单,如下所示:

    public Cmplx(double r, double i)
    {
        this.real = r;
        this.imag = i;
    }

    public double Real
    {
        get { return this.real; }
        set { this.real = value; }
    }

    public double Imag
    {
        get { return this.imag; }
        set { this.imag = value; }
    }
    public Cmplx Multiply(Cmplx b)
    {
        return new Cmplx((this.real * b.real - this.imag * b.imag), (b.imag * this.real + this.imag * b.real));
    }

    public Cmplx Divide(Cmplx b)
    {
        return new Cmplx((this.real * b.real + this.imag * b.imag) / (b.real * b.real + b.imag * b.imag), (b.real * this.imag - this.real * b.imag) / (b.real * b.real + b.imag * b.imag));
    }

    public Cmplx Scale(Double b)
    {
        return new Cmplx(this.real * b, this.imag * b);
    }

    public double Mod()
    {
        return Math.Sqrt(this.real * this.real + this.imag * this.imag);
    }

    public Cmplx Sqrt()
    {
        double r, theta;
        r = this.Mod();
        theta = this.Arg();

        return new Cmplx(Math.Sqrt(r) * (Math.Cos(theta / 2)), Math.Sqrt(r) * Math.Sin(theta / 2));
    }

    public Cmplx Exp()
    {
        double x, y, z;
        x = Math.Exp(this.real);
        y = Math.Cos(this.imag);
        z = Math.Sin(this.imag);

        return new Cmplx(x * y, x * z);
    } 

以下是我的 ZedGraph 方法,只有从 Codeproject ZedGraph 教程修改的 CreateGraph 方法:

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        SetSize();
    }

    private void SetSize()
    {
        zedGraphControl1.Location = new Point(10, 10);
        zedGraphControl1.Size = new Size(ClientRectangle.Width - 20,
                                ClientRectangle.Height - 20);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        CreateGraph(zedGraphControl1);
        SetSize();
    }

    private void CreateGraph(ZedGraphControl schrodinger)
    {
        GraphPane myPane = schrodinger.GraphPane;

        myPane.Title.Text = "Free Space Schrodinger Equation)";
        myPane.XAxis.Title.Text = "Time (s)";
        myPane.YAxis.Title.Text = "Psi";

        double alpha, beta, norm, k0, t;
        double L,N;

        L = 25;
        N = 4096;
        alpha = 0.5;
        beta = 0.5785;  
        k0 = 1;
        t = 0;
        norm = (Math.Exp(-k0 * k0 * alpha)) * (Math.Pow((alpha / (2 * Math.PI)), 0.25));

        PointPairList list1 = new PointPairList();

        for (double i = -L; i <= L; i -= (-2L) / N)
        {
            Cmplx gamma = new Cmplx(alpha, beta * t);
            Cmplx a = new Cmplx(2 * alpha * k0, 0);
            a.Imag = i;
            Cmplx b = a.Multiply(a);
            Cmplx phi = (((b.Divide(gamma.Scale(4))).Exp()).Scale(norm)).Divide(gamma.Sqrt());
            list1.Add(phi.Mod(),i);
        }

        LineItem myCurve = myPane.AddCurve("t=0",
              list1, Color.Red, SymbolType.Default);
        schrodinger.AxisChange();
    }

我改了

list1.Add(phi.Mod(), i); 

上述代码块的for循环内的一行

Console.WriteLine("{0}, {1}", phi.Mod(), i); 

尽管在命令行界面中,这仍按预期工作。使用 ZedGraph 库结果的 Windows 窗体应用程序在引发上述异常之前消耗了大约 1.7GB 的 RAM。

我通过绘制大量数据点发现了一个 ZedGraph 内存不足的实例

Charting massive amounts of data

但是,我不太倾向于我绘制的 4096 个数据点会导致这种内存消耗,而宁愿指责我(缺乏)编程技能

注意:这是一项学术作业,因此提供的任何帮助都应考虑到这一点。干杯!

编辑:修复了将 L 和 N 初始化为整数而不是双精度数的问题。如前所述,问题仍然存在。

EDIT2我是个白痴。我在我的测试 CLI 项目中将 L 和 N 的初始化更改为双精度,但未能更新 Windows 窗体应用程序的源代码。感谢 Jeppe Stig Nielsen 为我指明了正确的方向。

【问题讨论】:

    标签: c# zedgraph


    【解决方案1】:

    检查您的 for 循环。好像减一 (-) 太多了。

    此外,您的除法(-2*L) / N(缺少星号*)将给出,因为它是一个整数除法。要解决这个问题,请将一个因素转换为double,或者简单地写:

    2.0 * L / N
    

    请注意,.0 表示浮点数 (System.Double),这将强制“步骤”的整个计算根据需要使用浮点执行。

    总结一下:如果您的步数为零或方向错误,您将继续向list1 添加点,直到内存不足。也许这个for 循环更好(但你自己测试一下):

    for (double i = -L; i <= L; i += (2.0 * L) / N)
    

    【讨论】:

    • 我认为问题出在 for 循环上,即没有正确终止,但控制台应用程序的输出文件显示其他情况,即初始条目:1.44107617141494E-136,-25 -- 最终条目:1.44107617141494E -136, 25 表明 for 循环工作正常。也很抱歉,我把之前版本的 L 和 N 的初始化复制为整数(应该是双精度)。更新了上面的 OP。无论如何,问题仍然存在。
    • @user1748005 因为上面写了for循环,所以不会编译。你错过了一个星号*。您的for 循环可能没有问题,但如果您没有发布代码的正确版本,这很难猜到。从您提供的堆栈跟踪中可以很容易地看到,问题是您向list1 添加了太多点。底层List&lt;&gt; 在尝试调整大小以容纳所有这些点时内存不足。
    • 很抱歉给您带来了困惑。这是漫长的一天,到目前为止我的注意力还不是很出色 - 请参阅 OP 以获取有关我犯了什么错误的更多信息。有趣的是,该代码编译无星号并执行与星号类似的计算。当我将鼠标悬停在 VS 2010 中变量的初始化上时,当我将其声明为整数时,它显示为 System.Int32,当声明为双精度时,它显示为 System.Double;所以我明白你在说什么。以后我一定会坚持的。
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2021-05-20
    相关资源
    最近更新 更多