I wanted to show to a colleague that you can allocate more than 2GB of ram, so I made a little test application.
let mega = 1 <<< 20
let konst x y = x
let allocate1MB _ = Array.init mega (konst 0uy)
let memoryHog = Array.Parallel.init 8192 allocate1MB
printfn "I'm done..."
System.Console.ReadKey() |> ignore
this works and you actually see the process happily hogging away at the system's memory. However, it takes somewhat long - hence the Array.Parallel.init
.
I noticed, that the same code does not work, if I write it with
let allocate1MB _ = Array.zeroCreate mega
More precisely, no data is allocated and it takes no time.
So thus my question; What is the difference in semantics between Array.zeroCreate and Array.init?
I understand that Array.init
would run my konst 0uy
function each time, which would explain the time difference. But why does Array.zeroCreate
not allocate the memory?