# Memory Allocation on Array Element Operations

**URL:** <https://discourse.julialang.org/t/memory-allocation-on-array-element-operations/66676>\
**Category:** Performance\
**Tags:** question\
**Created:** [August 19, 2021, 1:39pm UTC](https://discourse.julialang.org/t/memory-allocation-on-array-element-operations/66676 "2021-08-19T13:39:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthiasSchneller](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@MatthiasSchneller](https://discourse.julialang.org/u/MatthiasSchneller)\
**Post date:** [August 19, 2021, 1:39pm UTC](https://discourse.julialang.org/t/memory-allocation-on-array-element-operations/66676/1 "2021-08-19T13:39:27Z")

</div>

Hi,  
I currently have the problem that i want to add elements from a Vector to other, more or less random, elements of a matrix. Because i have to do this a lot of times i wanted to improve the performance of it. And that’s when i realized that Julia does memory allocations when Elements of an Array are added. Like in the following example:

```julia
julia> a = ones(2);
julia> B = ones(2, 2);
julia> @allocated B[1, 2] += a[2]
48

```

My question now is, how can this allocation be avoided? Is there a way to do it? And a second question just out of interest, what is the reason for the allocation in the first place?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 19, 2021, 1:44pm UTC](https://discourse.julialang.org/t/memory-allocation-on-array-element-operations/66676/2 "2021-08-19T13:44:38Z")

</div>

because your variables are in global scope thus type instability forces Julia to perform boxing and unboxing:

```julia
julia> const a = ones(2);

julia> const B = ones(2, 2);

julia> @allocated B[1, 2] += a[2]
0

```

this doesn’t happen as soon as you start doing real operations inside functions

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 19, 2021, 1:44pm UTC](https://discourse.julialang.org/t/memory-allocation-on-array-element-operations/66676/3 "2021-08-19T13:44:53Z")

</div>

There isn’t really an allocation there, that is coming only from the printing of the result to the REPL (or the global scope of the variables as pointed above, or both, I’m not completely sure):

```julia
julia> using BenchmarkTools

julia> a = ones(2);

julia> B = ones(2, 2);

julia> @ballocated $B[1,2] += $a[2]
0

```

You can also verify that if you put that in a function that returns nothing:

```julia
julia> function up(a,B) 
         B[1,2] += a[2]
         nothing
       end
up (generic function with 1 method)

julia> @allocated up(a,B) # first run compiles
96518

julia> @allocated up(a,B)
0

```
