Large array allocation & out of memory error

Started by
4 comments, last by lwm 10 years, 6 months ago
I am having trouble with this prime number sieve running out of memory.

It fails at Values.Capacity = n; Previously Values was just an array defined like this : bool[] Values = new bool[n]; but had the same result. I am passing primesieve an int.MaxValue.
        static int[] Primesieve(int n)
        {
            List<bool> Values = new List<bool>();
            Values.Capacity = n;
            int[] primes = new int[(int)Math.Sqrt(n)];
            int primecount = 0;
            Values[0] = true;
            Values[1] = true;
            
            for (int i = 0; i < Math.Sqrt(n); i++)
            {
                if (Values[i] == false)
                {
                    for (int j = i * i; j < Values.Count; j += i)
                        Values[j] = true;
                    primes[primecount] = i;
                    primecount++;
                }
            }
            return primes;
        } 
I believed my problem to be that since they are bools, a value type they are being created on the stack so I did this to force heap allocation. but it still fails at the same place.
    

public class Reference<T>
{
    public T Ref;
}
static int[] Primesieve(int n) 
{ 
    List<Reference<bool>> Values = new List<Reference<bool>>();
    Values.Capacity = n; 
    int[] primes = new int[(int)Math.Sqrt(n)]; 
    int primecount = 0; 
    Values[0].Ref = true; 
    Values[1].Ref = true;
    for (int i = 0; i < Math.Sqrt(n); i++)
    {
        if (Values[i].Ref == false) 
        { 
            for (int j = i * i; j < Values.Count; j += i)
                Values[j].Ref = true;
            primes[primecount] = i;
            primecount++;
        }
    }
    return primes;
}
My machine has 16gb of ram running win7 64-bit and i even targeted the build specifically to x64. So I know it cat actually be running out of memory as int.MaxValue Booleans should only take around 2gb assuming .Net is not packing 8 of them into 1 byte, and if it is then it should only really take up 265mb.

Or am I some how still failing to use the heap?
Advertisement
Yes, there is something you are missing.

First, when you request bools it still allocates in individual bytes a full byte for each bool. A few languages (notably c++) tried to go the route of making bit arrays into compact containers, and it fails horribly. Each bool takes a full byte for many very good reasons. You can search online for custom containers for bit arrays if you want a compact representation.

You are passing 2^31 for your array length. That's about 2.1 billion entires, or 2GB of space, not 256MB. That won't work. The design of .net limits the maximum size of objects.

On .net 4.5, the maximum size of an array is 56 bytes less than 2^21 if you are on a 64-bit build.
On .net before 4.5, the actual value is system dependent and varies between runs, but the limit is about 1.4 billion single-byte entries, even on 64-bit builds.

There is a maximum object size in the CLR, even on 64-bit systems. There are a couple of possible solutions:

- Use a BitVector to avoid wasting 7/8 of the array:


static IEnumerable<int> Primesieve(int n)
{
    BitArray A = new BitArray(n);
    A.SetAll(true);
    A[0] = false;
    A[1] = false;

    for (int i = 2; i < Math.Sqrt(n); i++)
    {
        if (A[i])
        {
            for (long j = i * i; j < n; j += i)
            {
                A[(int)j] = false;
            }
        }
    }

    for (int i = 0; i < A.Length; i++)
    {
        if (A[i])
            yield return i;
    }
}

- Create a class that stores a large logical array as multiple chunks

- Use P/Invoke to allocate unmanaged memory (should be your last option)

current project: Roa

Oh, that BitArray did it! Thanks!

Or a memory mapped file, perhaps ?

Oh; and I believe a bool takes 4 bytes, sometimes, based on padding and word alignment imposed by the 32/64bit OS. I haven't checked this myself, but in general in .net there is no advantage in storing anything less than a full Int32 (shorts, bools etc may easily take a full 32bits), other than it being a more declarative coding style; again, please dont take this as a definitive answer.

Phillip


I haven't checked this myself, but in general in .net there is no advantage in storing anything less than a full Int32 (shorts, bools etc may easily take a full 32bits), other than it being a more declarative coding style

For local variables this may be the case. But that's an implementation detail.

In an array of bools, every element is exactly one byte though.

For bool fields in classes/structs the bool will probably be padded to 32 bits for performance reasons but you can tell the CLR to only use one byte if you really want to.

current project: Roa

This topic is closed to new replies.

Advertisement