Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash on Multidimensional Float Array Allocation

Tags:

c#

.net

I am attempting to allocate a very large multidimensional float array but an hitting out of memory exception issues when going above certain dimensions.

//Doesn't crash
float[,] newArr = new float[40000, 5000];

//Crashes
float[,] newArr1 = new float[45000, 5000];

//Doesn't crash
float[,] newArr2 = new float[40000000, 5];

//Crashes
float[,] newArr3 = new float[45000000, 5];

I am not sure what the issue is, I'm aware of array size limits of 2GB and 4 billion elements, but neither of these approaches either limit. Also, I cannot use another data struct because I need to pass the [,] to an external API call. Anyone have a clue what might be going on here? Thanks!

like image 750
DDushaj Avatar asked Apr 08 '26 08:04

DDushaj


2 Answers

What I have experienced with your code is that it doesn't work when compiling (and running) in 32-bits mode. If I switch to 64-bits build mode, it does work.

So open your project settings > Compile and set Platform target to x64.

like image 89
Patrick Hofman Avatar answered Apr 10 '26 20:04

Patrick Hofman


From Hans Passant:

45000 x 5000 x 4 ~= 900 megabytes. You are not going to get that in a 32-bit process, the largest hole in the available address space hovers around ~650 megabytes at startup and that goes rapidly downhill when your program has been running for a while. Just remove the jitter forcing so your program can run as a 64-bit process. Right-click your EXE project > Properties > Compile tab. Lots of really big holes in a 8 terabyte address space.

like image 25
DDushaj Avatar answered Apr 10 '26 21:04

DDushaj