Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong><em>Update:</strong> my <a href="https://stackoverflow.com/questions/1840481/how-can-i-know-the-actual-maximum-number-of-elements-a-net-array-of-a-given-type/1842456#1842456">other answer contains the solution</a> but I leave this in for the info about Mono, C#, the CLR links and the discussion thread</em></p> <p>The maximum size of an array is limited by the size of an integer, not by the size of the objects it contains. But any object in .NET is limited to 2GB, period (thanks to Luke and see EDIT), which limits the total size of your array, which is the sum of the individual elements plus a bit of overhead.</p> <p>The reason that it chokes your system is, well, the system's available memory. And the system of a win32 process only allows you to use 2GB of memory, of which your program and the CLR already use quite a bit even before you start your array. The rest you can use for your array:</p> <pre><code>int alot = 640000000; byte[] xxx = new byte[1U &lt;&lt; 31 - alot]; </code></pre> <p>It depends on how your CLR is configured whether or not you run out of memory. For instance, under ASP.NET you are bound by default to 60% of the total available memory of the machine.</p> <p>EDIT: <a href="https://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0/1088044#1088044">This answer to a related post</a> goes a bit deeper into the subject and the problems with 64 bit. It is possible on 64 bit systems, but only using workarounds. It points to <a href="http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx" rel="nofollow noreferrer">this excellent blog post on the subject</a> which explains <code>BigArray&lt;T&gt;</code>.</p> <p>NOTE 1: other CLR's, i.e. Mono's, simply allow larger then 2GB objects.</p> <p>NOTE 2: it is not the language that limits you. This compiles just fine in C#, but try and fine a machine that doesn't throw on it is a rather futuristic thought (and frankly, the field in the Array class holding the length is an <code>int</code>, which means this will <em>always</em> throw on 32 bit, but not necessarily, while extremely likely, on any 64 bit implementation):</p> <pre><code>int[] xxx = new int[0xFFFFFFFFFFFFFFFF]; // 2^64-1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload