# Growing memory with broadcast assignment

**URL:** https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206
**Category:** Performance
**Tags:** memory
**Created:** [December 21, 2020, 11:46pm UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206 "2020-12-21T23:46:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ancorso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ancorso/32/20447_2.png) [@ancorso](https://discourse.julialang.org/u/ancorso)
#### Post date: [December 21, 2020, 11:46pm UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/1 "2020-12-21T23:46:26Z")

</div>

Here is a MWE of the problem:

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

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

```

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?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 21, 2020, 11:56pm UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/2 "2020-12-21T23:56:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ancorso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ancorso/32/20447_2.png) [@ancorso](https://discourse.julialang.org/u/ancorso)
#### Post date: [December 22, 2020, 12:02am UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/3 "2020-12-22T00:02:32Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ancorso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ancorso/32/20447_2.png) [@ancorso](https://discourse.julialang.org/u/ancorso)
#### Post date: [December 22, 2020, 12:20am UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/4 "2020-12-22T00:20:28Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 22, 2020, 12:28am UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/5 "2020-12-22T00:28:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ancorso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ancorso/32/20447_2.png) [@ancorso](https://discourse.julialang.org/u/ancorso)
#### Post date: [December 22, 2020, 1:04am UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/6 "2020-12-22T01:04:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 22, 2020, 1:37am UTC](https://discourse.julialang.org/t/growing-memory-with-broadcast-assignment/52206/7 "2020-12-22T01:37:12Z")

</div>

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.
