27

I heard that there is a hard limit on the size of .Net Array. It is said that the maximum amount of memory that can be allocated to any single instance of an Array object ( regardless of whether it's int[], double[] or your own array) is 2GB. And no, if you have a 64 bit machine, the 2GB limit is still there.

I'm not sure whether my impression is correct or not. Anyone can confirm?

3
  • Wow, you started an interesting discussion. Commented Mar 10, 2010 at 8:47
  • If it makes you feel any better: the 2GB limit exists for unmanaged code as well. This is a constraint in the x64 instruction set, indexed offset addressing still has a 32-bit limit for the offset. It is not that it can't be overcome, it is just very inefficient to do so. Commented Mar 10, 2010 at 9:18
  • 1
    Hans Passant: I am using C++ unmanaged code in my x64 app to allocate an array of 600E6 doubles using malloc. That is 4.8E9 bytes. Commented Aug 27, 2014 at 0:26

6 Answers 6

24

In versions of .NET prior to 4.5, the maximum object size is 2GB. From 4.5 onwards you can allocate larger objects if gcAllowVeryLargeObjects is enabled. Note that the limit for string is not affected, but "arrays" should cover "lists" too, since lists are backed by arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Mark Gravell. We remembered this was a thing, but forgot what it was called.
15

That is correct. No single object can be larger than 2 GB.

As with 32-bit Windows operating systems, there is a 2GB limit on the size of an object you can create while running a 64-bit managed application on a 64-bit Windows operating system.

This question has additional details and some useful links: Single objects still limited to 2 GB in size in CLR 4.0?

6 Comments

Is there also a limit on the number of elements in the array?
@uriDium: effectively yes, since an array is one object.
This question has additional info on the size of arrays in .NET stackoverflow.com/questions/1589669/overhead-of-a-net-array/…
@Brain, just did the maths, i know in java the minimum class size is 8 bytes. At 8 bytes 2147483648 is almost 17 gig. So i guess we don't even get to the index being an integer as a problem. Yet...
This restriction is lifted as of .NET 4.5: msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx.
|
4

You will run into a practical limit first - it is pretty impossible to get a 2gb array allocated. Practical limits I have encountered are around the 800mb mark AT PROGRAM START - going down drastically after that.

Anything larger than 64mb is a luck gamble on 32 bit - the large object heap is not defragmented, so you need 65mb free in one piece or allocation fails.

Theoretical limits are:

  • usable memory, especially under 32 bit.
  • 32 bit number space for index (0 upward - no negative numbers for arrays UNLESS YOU PLAY SMART IN CREATION). You can create arrays allowing negative numbers, but not with C# standard syntax - only with reflection.
  • 2gb per object.

But seriously, the practical implications are larger.

For .NET 4.0.... consider using memory mapped files ;)

Comments

2

Since .NET 6, the maximum number of elements an array can hold is defined by Array.MaxLength. It is currently 0x7FFFFFC7.

While strings are array-like, they have a lower limit. Currently the longest you cam make a string is 0x3FFFFFDF.

Comments

1

I would have thought that the limit might be on the index. I thought that index used has to be an integer so anything bigger than integer wouldn't work unless they have some way around that. So that would be 4294967296 elements. Not sure if this is even half true. I would like to know the answer myself.

EDIT: As tomtom pointed out, integer is usually signed unless they using a non signed integer. So half of 4294967296 or 2147483648 roughly.

7 Comments

it is pretty much double true. As arrays are integer indexed.... and start at 0 - only the positive part of the index is usable, roughly half your number ;) So, your answer is double true ;)
If the index is an unsigned integer it would be your number.
@TomTom: No it isn't. Each array is one object and since objects are restricted to 2 GB it depends on the type of elements the array holds.
@uriDium: The 2 GB limit will restrict this number significantly. You cannot have 2147483648 elements in an array.
@Brian Rasmussen: 2147483648 is the theoretical maximum number of elements, if each element would be 1 Byte long.
|
0

Hope this help: http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx

i.e.

  1. It uses int as index, which has max value = 2,147,483,647 (2GB)
  2. Its by design. 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.