0

I understand a similar question has been asked many times and answered as well. Much of the articles on internet point to only one answer i,e. max of int value or 2147483647.

Inline are couple of links from stackoverflow:

What is the Maximum Size that an Array can hold?

Maximum size of string array in C#

However, when I tried to check the same an OutOfMemoryException is thrown. In fact I tried to reduce the size by a factor of 2 to 1073741823(throws exception), factor of 4 to 536870911(throws exception), factor of 8 to 268435455 (program executes well without any exception).

Test Code:

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            int arraySize = Int32.MaxValue / 1; //Factor when set to 8, program executes without any exception
            int[] testArray = new int[arraySize];

            testArray[0] = 6;

            Console.WriteLine("Press Enter to finish.");
            Console.ReadLine();
        }
    }
}

Looks like all the other answers point to a theoretical value which is `2147483647. However in practice this is not always the case. So the question is, given the application code is very simple with just one array and couple of calls to console, is it possible to figure out at what size of array the application will throw an OutOfMemoryException.

13
  • Which .NET version? What bitness? Do you have gcAllowVeryLargeObjects? The point is that your application needs to be able to allocate a contiguous amount of arraySize*elementSize of RAM. If it can't, it'll fail. AFAIK it's not trivial using managed code to predict whether that will succeed. What do you need that large an array for anyway? Commented Aug 12, 2020 at 10:11
  • Int32.MaxValue = 2147483647 which is larger than the max size of an array. Commented Aug 12, 2020 at 10:14
  • That first question you linked gives you the answer you need, did you read it all? Commented Aug 12, 2020 at 10:15
  • 1
    Ultimately, and I cannot emphasize this enough: having huge vectors is usually a bad idea and is going to hurt you, so asking "what is the largest array that I can theoretically have" is usually a sign that someone is (metaphorically) pointing a shotgun at their foot, and asking what the most powerful shell they can theoretically use is. There are some scenarios where it is relevant, but they are niche, and often borrowing unmanaged memory (perhaps using Memory<T>/Span<T>) is a better idea. Commented Aug 12, 2020 at 10:16
  • 1
    If you try an array size of something like 536,870,850, that will probably work (it puts you just below the 2GB limit for single objects). However you can't guarantee that it will work, as the OS might not be able to allocate you enough contiguous memory. There's no guarantee that any allocation will succeed, but the likelihood of failure goes up the larger the allocation Commented Aug 12, 2020 at 10:22

1 Answer 1

2

The NET framework limits the maximum size of any object to 2 GB by default. Arrays can be bigger on 64-bit platforms if you enable the corresponding setting gcAllowVeryLargeObjects.

So theoretically, the limit of an int array should be around 536 870 912 elements, but that doesn't account for the memory footprint of the array itself, therefore the real limit must be less.

Another issue is that arrays need to be allocated in contiguous memory space. If the allocator can't find any such space, you will get an OutOfMemoryException even if the object is under the maximum size limit.

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

1 Comment

Thank you @InBetween. The "536 870 912" makes sense for me. I missed out on the math of 2GB must be divided 4. That is a valuable point for me.

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.