在do while循环中抛出System.OutOfMemoryException ...(在for循环中工作) [英] System.OutOfMemoryException thrown in do while loop...(worked in for loop)

查看:76
本文介绍了在do while循环中抛出System.OutOfMemoryException ...(在for循环中工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个计算列。最初这个列是用for循环构造的,值是硬编码的。这个用于测试目的,但需要进行更改,以便用户可以输入不同的值。



问题是计算有限制。有一个根,你根本不能有零以下的东西,所以我一直试图找到一种方法来达到限制之前停止循环。



i尝试运行do-while循环并不断出现内存异常,显然我有3 + gb物理内存。



完全不确定该怎么做,谢谢你的帮助



这里是我的首字母循环:

i am trying to create a calculated column. originally the column was constructed with a for loop and values were hard coded in. this WORKED for testing purposes, however changes needed to be made so that users could input different values.

the problem is that the calculation has a limit. there's a root and you can't have anything below zero in the root, so i have been trying to figure out a way to stop the loop before hitting that limit.

i tried running the do-while loop and keep getting outof memory exceptions and apparently i have something like 3+gb physical memory.

thoroughly unsure what to do, thank you for any help

here's my initial for loop:

public DataTable sphCalcBCZone1(decimal b2, decimal b5, decimal b16)
{
    DataTable dt = new DataTable();
    DataColumn c = new DataColumn("Z1_BC");
    c.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c);
    decimal stepPoint = 0M;
    double stepd = Convert.ToDouble(stepPoint);
    decimal gobbledegook = 8.6M;
    for (stepd = 0; stepPoint <= gobbledegook; stepPoint = stepPoint + .005M)
    {
        DataRow row = dt.NewRow();
        double stepd = Convert.ToDouble(stepPoint); double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
        double valC1 = Math.Pow(stepd, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(stepd, 2))) + b16d;
        decimal valC2 = Convert.ToDecimal(valC1);
        row["Z1_BC"] = valC2; dt.Rows.Add(row);
    }
    return dt;
}





这是我的do-while循环,其中我试图放入限制并且我得到了outofmemory异常



here's my do-while loop in which i tried to put in the "limit" and i got the outofmemory exception

public DataTable sphCalcBCZone1(decimal b2, decimal b5, decimal b16)
    {
        DataTable dt = new DataTable();
        DataColumn c = new DataColumn("Z1_BC");
        c.DataType = System.Type.GetType("System.Decimal");
        dt.Columns.Add(c);
        double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
        double limit = Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2);
        do
        {
            DataRow row = dt.NewRow();
            double valC1 = Math.Pow(step, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2))) + b16d;
            decimal valC2 = Convert.ToDecimal(valC1);
            row["Z1_BC"] = valC2; dt.Rows.Add(row);

//forgot to add this line...now value is too large/small for decimal
step = step + .005;


        } while (limit > 0);
        return dt;
    }

推荐答案

你永远不会改变限制所以它'无限循环
You're never changing limit so it'll loop infinitely


查看 Do / While 循环内部。你是否看到修改你用来控制循环的变量的任何东西,你称之为 limit



目前,它将永远运行并实际运行内存,将行添加到 DataTable
Look inside your Do/While loop. Do you see anything in the that modifies the variable you're using to control the loop, you called it "limit".

Currently, it will go forever and actually run the process out of memory adding rows to a DataTable.


谢谢大家好。



这里是完成的位运行



thank you all.

here's the finished bit that runs

DataTable dt = new DataTable();
    DataColumn c = new DataColumn("Z1_BC");
    c.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c);
    double step = 0;
    double limit;
    double b2d = Convert.ToDouble(b2); double b5d = Convert.ToDouble(b5); double b16d = Convert.ToDouble(b16);
    do
    {

        limit = Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2);
        DataRow row = dt.NewRow();
        double valC1 = Math.Pow(step, 2) / (b2d + Math.Sqrt(Math.Pow(b2d, 2) - b5d * Math.Pow(step, 2))) + b16d;
       // decimal valC2 = Convert.ToDecimal(valC1);
        row["Z1_BC"] = valC1; dt.Rows.Add(row);
        step = step + .005;
    } while (limit > .000000000001);
    return dt;


这篇关于在do while循环中抛出System.OutOfMemoryException ...(在for循环中工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆