简体   繁体   中英

Unsafe C# to bypass the 2GB limit

As I understand there is a limit in arrays and other objects to a maximum of 2GB in .NET. I know that in 64bit machines we can use gcAllowVeryLargeObjects to get around this issue and create arrays of bigger than 2GB.

My question is whether we can use the unsafe block in C# to create an unsafe array of bigger than 2GB size (ie a 3GB array) in a 32bit machine.

While in theory you may be able to allocate large arrays in 32bit process in practice you'll run into address space fragmentation issues. I'd recommend finding some other approach as finding more than 2GB of continuous address space in 32bit process is rare.

Note that you'll also need to make sure process have "large address space aware" flag (I think by default .Net binaries have it, but check anayway).

No, that is not possible, but it's not a limitation in C# or its array implementation.

The total address space for a 32 bit machine is 4 GB. Some of that is used for hardware access, so the usable address space is about 3.5 GB, varying a bit depending on the memory hardware in the machine.

Normally the address space above 2 GB is reserved for the system, so you only have 2 GB of usable address space for your application data. As that includes your program, the stack and the heaps, there isn't even 2 GB available for the large objects heap.

It might be possible to allow more then 2 GB of the memory for user data, but still not all of the 3.5 GB as the system still needs some, and with the other things you have in memory you won't ever get much more than 2 GB available for a large object allocation.

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