# Code uses excessive memory when using \`\`\`.+\`\`\` instead of a loop?

**URL:** https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624
**Category:** New to Julia
**Tags:** memory-allocation
**Created:** [October 5, 2023, 5:54pm UTC](https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624 "2023-10-05T17:54:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![felipeh](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@felipeh](https://discourse.julialang.org/u/felipeh)
#### Post date: [October 5, 2023, 5:54pm UTC](https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624/1 "2023-10-05T17:54:37Z")

</div>

In debugging some code I wrote, I noticed that there seems to be a significant difference between `x = x .+ 1` and writing the loop out explicitly. In particular, the following code runs in 0.006 seconds and reports only 2 allocations totaling 64KiB.

```julia
function main()
    N = 5000
    M = 2^13
    x = zeros(M)
    for _ in 1:N
        for j in 1:M
            x[j] = x[j] + 1
        end
        #x = x .+ 1
    end
end

@time main()

```

However, if I uncomment the line `x = x .+ 1`, the code runs in 0.3 seconds and reports 10k allocations totaling 300 MiB. Moreover, if I increase `N` to 20000 the memory usage increases to 1.2GiB – the array does not change size just the number of iterations. Why is the memory usage so large? Should I avoid using broadcasting in loops for this reason? I assume I am making some fundamental mistake.

If it helps, I am using julia version 1.9.3.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [October 5, 2023, 5:57pm UTC](https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624/2 "2023-10-05T17:57:36Z")

</div>

You want `x .= x .+ 1`. This modifies `x` in-place. Whereas `x = x .+ 1` allocates a new array and _then_ assigns it to `x`.

---

<div class="post-metadata">

### Author: ![felipeh](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@felipeh](https://discourse.julialang.org/u/felipeh)
#### Post date: [October 5, 2023, 5:58pm UTC](https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624/3 "2023-10-05T17:58:23Z")

</div>

Excellent! Thank you so much, that fixes the problem.

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [October 6, 2023, 3:13am UTC](https://discourse.julialang.org/t/code-uses-excessive-memory-when-using-instead-of-a-loop/104624/4 "2023-10-06T03:13:59Z")

</div>

> [@felipeh](#):
>
> ```julia
> function main()
> N = 5000
> M = 2^13
> x = zeros(M)
> for _ in 1:N
> for j in 1:M
> x[j] = x[j] + 1
> end
> #x = x .+ 1
> end
> end
> 
> ```

Small note–using `for j in 1:M` is generally bad practice, because it’s slower and more error-prone than `for j in eachindex(x)`. It causes bugs when the array indices aren’t `1:M`. (Here they are so it’s OK, but that’s not always the case, so avoiding ranges is a good habit.)
