Maximum length of byte[]?
Asked Answered
L

5

26

I'm trying to create an array of bytes whose length is UInt32.MaxValue. This array is essentially a small(ish) in-memory database:

byte[] countryCodes = new byte[UInt32.MaxValue];

On my machine, however, at run-time, I get a System.OverflowException with "Arithmetic operation resulted in an overflow".

What's the deal? Do I need to use an unsafe block and malloc? How would I do that in C#?

Linders answered 15/10, 2010 at 16:45 Comment(1)
I had no idea there were so many countries!Woolgathering
B
38

The current implementation of System.Array uses Int32 for all its internal counters etc, so the theoretical maximum number of elements is Int32.MaxValue.

There's also a 2GB max-size-per-object limit imposed by the Microsoft CLR.

A good discussion and workaround here...

And a few related, not-quite-duplicate, questions and answers here...

Barranca answered 15/10, 2010 at 16:48 Comment(0)
P
15

On .NET 4.5 The maximum instantiatable length of a byte array is: 2147483591, or 56 less than int.MaxValue. Found via:

for (int i = int.MaxValue; i > 0; i--)
{
    try
    {
        byte[] b = new byte[i];
        Console.Out.WriteLine("MaxValue: " + i);
        Environment.Exit(0);
    }
    catch (Exception ignored)
    {}
}
Periodate answered 21/8, 2013 at 0:31 Comment(0)
B
3

Maximum length of a byte array is: 2130702268. for example:

var countryCodes = new byte[2130702268];
Belloir answered 29/12, 2018 at 6:29 Comment(0)
D
0

I wouldn't do this in the first place. Why would you want to set all that memory aside for this in-memory database? Wouldn't you rather want either a data structure which size increments as you go along (e.g. List<int>)? Or (if preferred) use an in-memory database like sqlite?

Discount answered 15/10, 2010 at 17:11 Comment(0)
F
0

Use Array.MaxLength:

var countryCodes = new byte[Array.MaxLength];

Pressing F12 on Array.MaxLength reveals the value:

public static int MaxLength => 0X7FFFFFC7;

which is 2,147,483,591 bytes (56 bytes less than int.MaxValue)

Fredia answered 26/4 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.