Friday, September 21, 2012

Allocating arrays larger than 2GB in .Net

Prior to .Net 4.5, one could create arrays of a max size of 2gb even on 64 bit machines. (On 32 bit machines, this is a theoretical limit which is very hard to hit in practice, because of the level of fragmentation of the memory).

In .Net 4.5, one can now allocate arrays that are greater than 2gb in size. But before you can do that, you need to enable a setting on the machine’s runtime settings. To enable it you need to set “gcAllowVeryLargeObjects” to true. (Its set to false by default).

More info:

<gcAllowVeryLargeObjects> Element: http://msdn.microsoft.com/en-us/library/hh285054(v=VS.110).aspx

Runtime performance improvements in .Net 4.5:http://blogs.msdn.com/b/somasegar/archive/2012/05/16/net-improvements-for-cloud-and-server-applications.aspx

The GC changes were made in recognition of the importance of memory management for various kinds of server workloads.  Another such change has to do with array sizes.  In .NET 4 and earlier, no object could be larger than 2GB in size, even in 64-bit processes.  For certain workloads that use large arrays, however, this size limit can be constraining.  As such, .NET 4.5 supports lifting the limit for arrays in 64-bit processes, such that arrays may be larger than 2GB.  This means, for example, that you could allocate an array of Int32.MaxValue Int32s, even though such an array will consume 8GB.  Large objects in .NET are allocated on a special heap, known not coincidentally as the Large Object Heap (LOH);  .NET 4.5 has also seen significant performance improvements to the LOH, including usage of better algorithms for managing free memory in the heap.

-Somasegar

No comments: