简体   繁体   中英

Why the Allocation of an array with int.MaxValue fails and the allocation of two arrays with the size int.MaxValue / 2 does not?

Why the allocation of a byte array with the size int.MaxValue fails

    byte[] array1 = new byte[int.MaxValue]; // throws an OutOfMemoryException 

and the allocation of two arrays with the size int.MaxValue / 2 does not?

    byte[] array2 = new byte[int.MaxValue / 2];
    byte[] array3 = new byte[int.MaxValue / 2];

The maximum size of an object in .NET is 2GB: http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx

int.MaxValue + the array overhead is slightly greater than 2 GB.

我不知道c#中int.MaxValue的值,但我认为.Net不允许大于2GB的对象

在.NET的最新版本(至少4.5以上)中,您可以分配大于2Gb的对象 - 但是您必须设置gcAllowVeryLargeObjects - https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110 )的.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM