不能创造巨大的数组 [英] Can't create huge arrays

查看:120
本文介绍了不能创造巨大的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多其他的程序员,我走进素数,并作为他们来说,我喜欢的是挑战,所以我不是在寻找像的注释阿特金这样做比你老兄的速度更快,但只是一个解决办法 - 或者至少是一个暗示 - 我的问题。

Like many other programmers, I went into primes, and as many of them, what I like is the challenge, so I'm not looking for comment like Atkin did this faster than you dude, but just a solution - or at least an hint - to my issue.

我需要创建阵列(如大小> int.MaxValue )。于是,我去了很多的网页,发现 gcAllowVeryLargeObjects元素之一。我以为我得救了,添加以下魔法我的的App.config

I need to create big arrays (like size > int.MaxValue). So I went to a lot of web pages and found the gcAllowVeryLargeObjects Element one. I thought I was saved, add the following magic to my App.config:

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>

但它的没有的工作。这里的code我使用:

But it didn't worked. Here's the code I use :

void go(object sender, EventArgs eventArgs)
{
    t.Stop();
    ulong maxprime = 10;
    Stopwatch stopwatch = new Stopwatch();
    string s = String.Empty;
    while (maxprime < ulong.MaxValue)
    {
        stopwatch.Restart();
        richTextBox2.Text += Environment.NewLine + ("Max \t= " + maxprime.ToString("N0"));
        try
        {
            richTextBox2.Text += Environment.NewLine + ("Count \t= " + GetAllPrimesLessThan(maxprime).Count);
            richTextBox2.Text += Environment.NewLine + ("Time \t= " + stopwatch.Elapsed);
            richTextBox2.Text += Environment.NewLine + ("--------------------------------");
            maxprime *= 10;
            richTextBox2.Refresh();
        }
        catch (Exception exception)
        {
            s = exception.Message + "; Allocation size: " + (maxprime + 1).ToString("N0");
            break;
        }

    }
    if (!string.IsNullOrEmpty(s))
    {
        richTextBox2.Text += Environment.NewLine + s;
    }
    richTextBox2.Text += Environment.NewLine + ("Done.");
}

private static List<ulong> GetAllPrimesLessThan(ulong maxPrime)
{
    var primes = new List<ulong>() { 2 };
    var maxSquareRoot = Math.Sqrt(maxPrime);
    var eliminated = new bool[maxPrime + 1];

    for (ulong i = 3; i <= maxPrime; i += 2)
    {
        if (!eliminated[i])
        {
            primes.Add(i);
            if (i < maxSquareRoot)
            {
                for (ulong j = i * i; j <= maxPrime; j += 2 * i)
                {
                    eliminated[j] = true;
                }
            }
        }
    }
    return primes;
}

这此输出:

[...]
Max     = 1 000 000 000
Count   = 50847534
Time    = 00:00:15.3355367
--------------------------------
Max     = 10 000 000 000
Array dimensions exceeded supported range.; Allocation size: 10 000 000 001
Done.

如何才能摆脱这种错误的?

How can I get rid of this error?

FYI:我有


  • 16GB内存;

  • 32GB内存映射(/分页?)在SSD上;

  • 启用64位

推荐答案

从你的链接:

在使用你的应用程序配置文件中该元素可使数组,其超过2 GB大小,但不会改变物体的大小或数组大小其他限制:

Using this element in your application configuration file enables arrays that are larger than 2 GB in size, but does not change other limits on object size or array size:

在任何单一维度的最高指数为2147483591(0x7FFFFFC7)用于其他类型的字节数组和单字节结构数组和2146435071(0X7FEFFFFF)。

The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types.

又见<一个href=\"http://stackoverflow.com/questions/2338778/what-is-the-maximum-length-of-an-array-in-net-on-64-bit-windows\">What在64位Windows 在.NET中的数组的最大长度:

See also What is the maximum length of an array in .NET on 64-bit Windows:

这是数组,理论上最多有2,147,483,647元,,因为它使用索引的int

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing.

这篇关于不能创造巨大的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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