Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create huge arrays

Tags:

arrays

c#

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.

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>

But it didn't work. 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;
}

Which output this:

[...]
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: I've got

  • 16GB ram;
  • 32GB memory mapped(/paged?) on SSD;
  • 64bits enabled
like image 556
Thomas Ayoub Avatar asked Jun 17 '15 15:06

Thomas Ayoub


People also ask

How do you declare a large array?

Usually you need to create such an array dynamically on the heap. int *integer_array = (int*)malloc(2000000 * sizeof(int)); float *float_array = (float*)malloc(2000000 * sizeof(float)); The array might be too large for stack allocation, e.g. if used not globally, but inside a function.

What is the maximum size of array we can make?

The maximum allowable array size is 65,536 bytes (64K). Reduce the array size to 65,536 bytes or less. The size is calculated as (number of elements) * (size of each element in bytes).

Is it possible to increase array size?

Arrays can either hold primitive values or object values. An ArrayList can only hold object values. You must decide the size of the array when it is constructed. You can't change the size of the array after it's constructed.

Can you increase array size in C++?

You can't change the size of the array, but you don't need to. You can just allocate a new array that's larger, copy the values you want to keep, delete the original array, and change the member variable to point to the new array. Allocate a new[] array and store it in a temporary pointer.


2 Answers

From your link:

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:

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.

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

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

like image 103
CodeCaster Avatar answered Oct 18 '22 04:10

CodeCaster


If you hit the bounds of the integer max range, you can opt to use a long-index-based array.

The problem is that this isn't supported by the C# indexer properties, which uses int. You can build them by hand though by using Array.CreateInstance(Type, long[]).

Note you have to get the values using Array.GetValue(long).

like image 33
Patrick Hofman Avatar answered Oct 18 '22 04:10

Patrick Hofman