• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Large array allocation & out of memory error

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Grain  Members

Posted 29 September 2013 - 12:22 AM

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?

Edited by Grain, 29 September 2013 - 12:31 AM.

### #2frob  Moderators

Posted 29 September 2013 - 01:49 AM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #3lwm  Members

Posted 29 September 2013 - 01:49 AM

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;
}
}


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

Edited by lwm, 29 September 2013 - 01:51 AM.

current project: Roa

### #4Grain  Members

Posted 29 September 2013 - 02:25 AM

Oh, that BitArray did it! Thanks!

### #5PhillipHamlyn  Members

Posted 30 September 2013 - 01:31 PM

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

### #6lwm  Members

Posted 30 September 2013 - 02:11 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.