After reading Are C# Strings (and other .NET API’s) limited to 2GB in size? I played around with large strings and arrays in .NET 3.5. I found that the largest array I could allocate was int.MaxValue - 56
bytes. Similar thing for strings: the largest I could get was (int.MaxValue - 58) / 2
characters (since each character took 2 bytes). After that it throws OutOfMemoryException
.
Why does this limitation exist? Not that I've ever run into it in practice - I'm just curious about the inner workings of .NET.
Yes, this was on a 64-bit machine with plenty of RAM, of course - and yes, the process was running as a 64-bit process. (I could actually allocate 3 such arrays or strings for a total memory usage of 6GB.)
It was a design decition to restrict the size of an object on the GC Heap to 2GB, even on x64. Good blog post on it here from one of those involved in the design decision: