Growing memory with broadcast assignment

Here is a MWE of the problem:

function foo!(v)
    i = rand(1:size(v, 4) - 3)
    v[:,:,:,i:i+3] .= 1.

v = Array{Float32}(undef, 105, 80, 4, 50000)
for i=1:50000

When I run the code, julia’s memory footprint grows on each iteration despite the preallocation. Any ideas about what might be going on here?

How are you executing the code and inspecting the memory usage? I don’t see the same behavior on my Mac (Julia v1.5).

1 Like

Also running Julia v1.5 on a mac. Executing it using Juno/Atom in a global scope. Just inspecting memory using the system monitor. Any suggestions for a better way of inspecting memory usage?

If I replace the random index with i = 1 then the problem goes away. So maybe it has something to do with the creation of the different UnitRange objects on each call?

On 1.5 that should be stack allocated (so basically free).

Figured out the problem. I guess the expression v = Array{Float32}(undef, 105, 80, 4, 50000) does not actually allocate the full block of memory because of the undef initializer. It was therefore allocating as it was used which made the memory footprint grow up to what it would have if the allocation actually happened at the beginning.

1 Like

Oh. That makes sense. Operating systems can pretend to give you memory but sometimes only actually give it to you once you write to it.