相关疑难解决方法(0)

大数组C#OutOfMemoryException

对于我在C#中的编程练习,我试图创建一个long数组,长度为0x1fffffff(base10中为536,870,911),但是我得到了System.OutOfMEmoryException.

对于构建,我针对x64系统,我在Windows7 x64上运行VisualStudio2008,内存为8GB.它应该是数组的足够内存(它适用于JDK x64和CPP项目)

有什么想法吗 ?

        const long MAX = 0x1fffffff; // 536870911 in base10
        program.arr = new long[MAX];
        for (long i = 0; i < MAX; i++)
        {
            program.arr[i] = i;                
        }
Run Code Online (Sandbox Code Playgroud)

c# out-of-memory

7
推荐指数
2
解决办法
6233
查看次数

分配大型阵列; OutOfMemoryException VS OverflowException

考虑以下:

long size = int.MaxValue;
long[] huge = new long[size];     // throws OutOfMemoryException
long[] huge = new long[size + 1]; // throws OverflowException
Run Code Online (Sandbox Code Playgroud)

我知道单个对象的大小有2GB的限制,这解释了第一个异常,但是为什么一旦元素数量超过32位,我会得到一个不同的异常?

(如果这很重要,我正在使用64位计算机).

编辑:我也可以定义和使用一个long没有问题的索引器:

internal sealed class MyClass
{
   public object this[long x]
   { 
      get
      {
         Console.WriteLine("{0}", x);
         return null;
      }
   }
}

...

long size = int.MaxValue;
MyClass asdf = new MyClass();
object o = asdf[size * 50]; // outputs 107374182350
Run Code Online (Sandbox Code Playgroud)

.net c#

6
推荐指数
2
解决办法
3129
查看次数

记录非常大的数字

我正在处理BigInteger类,其数字大约为2,上升到10,000,000.

BigInteger Log函数现在是我算法中最昂贵的函数,我正在拼命寻找替代方案.

因为我只需要日志的组成部分,所以我遇到了这个答案,这在速度方面看起来很棒,但由于某些原因我没有得到准确的值.我不关心小数部分,但我确实需要得到一个准确的积分部分,无论该值是浮动还是上限,只要我知道哪个.

这是我实现的功能:

public static double LogBase2 (System.Numerics.BigInteger number)
{
    return (LogBase2(number.ToByteArray()));
}

public static double LogBase2 (byte [] bytes)
{
    // Corrected based on [ronalchn's] answer.
    return (System.Math.Log(bytes [bytes.Length - 1], 2) + ((bytes.Length - 1) * 8));
}
Run Code Online (Sandbox Code Playgroud)

除角落情况外,这些值现在非常准确.值7到7.99999,15到15.9999,23到23.9999 31到31.9999等返回-Infinity.数字似乎围绕字节边界.知道这里发生了什么吗?

例:

LogBase2(                    1081210289) = 30.009999999993600 != 30.000000000000000
LogBase2(                    1088730701) = 30.019999999613300 != 30.000000000000000
LogBase2(                    2132649894) = 30.989999999389400 != 30.988684686772200
LogBase2(                    2147483648) = 31.000000000000000 != -Infinity
LogBase2(                    2162420578) = 31.009999999993600 != -Infinity
LogBase2(                    4235837212) = …
Run Code Online (Sandbox Code Playgroud)

.net c# logarithm biginteger

4
推荐指数
1
解决办法
1481
查看次数

标签 统计

c# ×3

.net ×2

biginteger ×1

logarithm ×1

out-of-memory ×1