# Why is there an allocation?

**URL:** https://discourse.julialang.org/t/why-is-there-an-allocation/85405
**Category:** General Usage
**Tags:** memory-allocation
**Created:** [August 6, 2022, 4:55pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405 "2022-08-06T16:55:33Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 4:55pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/1 "2022-08-06T16:55:33Z")

</div>

```julia
module mbas001
elxs = fill(Matrix{Float64}(undef, 3, 3), 7)
for _i in eachindex(elxs)
    elxs[_i] = rand(3, 3)
end
ex = fill(zero(Float64), 3, 3) 
copy2ex!(ex, elxs, j, k) = let
    elxsc = elxs[c]
    for _i in axes(ex, 2)
        ex[1, _i] = elxsc[j, _i]; 
        ex[2, _i] = elxsc[k, _i]; 
    end
    ex
end
c = 4
@time let
    ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
end
@time copy2ex!(ex, elxs, 1, 2)
@time copy2ex!(ex, elxs, 1, 2)
@time copy2ex!(ex, elxs, 1, 2)
end

```

produces

```julia
julia> include("C:\\Users\\pkonl\\Documents\\00WIP\\HelmBEM.jl\\test\\littletest.jl")                                                                       
WARNING: replacing module mbas001.                                            
  0.000023 seconds (6 allocations: 224 bytes)                                 
  0.018632 seconds (9.56 k allocations: 517.691 KiB, 99.73% compilation time) 
  0.000017 seconds (6 allocations: 96 bytes)                                  
  0.000016 seconds (6 allocations: 96 bytes)                                  
Main.mbas001  

```

What puzzles me is why the function `copy2ex!` allocates memory.  
It happens in the loop

```julia
    for _i in axes(ex, 2)
        ex[1, _i] = elxsc[j, _i]; 
        ex[2, _i] = elxsc[k, _i]; 
    end

```

Which, in my understanding, should be simply assignment of one matrix location the value of another matrix location. In other words, scalars.

---

<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: [August 6, 2022, 4:57pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/2 "2022-08-06T16:57:13Z")

</div>

`c` is a non-const global variable.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 5:06pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/3 "2022-08-06T17:06:21Z")

</div>

Wow. I missed that! Thanks.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 5:08pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/4 "2022-08-06T17:08:44Z")

</div>

Now if I could understand why the memory allocation happens here:

```julia
ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:];

```

There are still 6 ALLOCATIONS:

```julia
WARNING: replacing module mbas001.                                            
  0.000025 seconds (6 allocations: 224 bytes)                                 
  0.022149 seconds (6.53 k allocations: 337.683 KiB, 99.85% compilation time) 
  0.000015 seconds                                                            
  0.000003 seconds                                                            
Main.mbas001 

```

The code looks like this now:

```julia
module mbas001
elxs = fill(Matrix{Float64}(undef, 3, 3), 7)
for _i in eachindex(elxs)
    elxs[_i] = rand(3, 3)
end
ex = fill(zero(Float64), 3, 3) 
copy2ex!(ex, elxs, c, j, k) = let
    elxsc = elxs[c]
    for _i in axes(ex, 2)
        ex[1, _i] = elxsc[j, _i]; 
        ex[2, _i] = elxsc[k, _i]; 
    end
    ex
end
@time let
    c = 4
    ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
end
c = 4
@time copy2ex!(ex, elxs, c, 1, 2)
@time copy2ex!(ex, elxs, c, 1, 2)
@time copy2ex!(ex, elxs, c, 1, 2)
end

```

---

<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 6, 2022, 5:21pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/5 "2022-08-06T17:21:40Z")

</div>

> [@PetrKryslUCSD](#):
>
> `elxs = fill(Matrix{Float64}(undef, 3, 3), 7)`

const

> [@PetrKryslUCSD](#):
>
> `ex = fill(zero(Float64), 3, 3) `

const

---

<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 6, 2022, 5:22pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/6 "2022-08-06T17:22:22Z")

</div>

> [@PetrKryslUCSD](#):
>
> ```julia
> @time let
> c = 4
> ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
> end
> 
> ```

```julia
@time @views let

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 5:32pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/7 "2022-08-06T17:32:41Z")

</div>

> [@jling](#):
>
> `@views `

Now everything is a local variable. Still, there are allocations.

```julia
module mbas001
let
elxs = fill(Matrix{Float64}(undef, 3, 3), 7)
for _i in eachindex(elxs)
    elxs[_i] = rand(3, 3)
end
ex = fill(zero(Float64), 3, 3) 
copy2ex!(ex, elxs, c, j, k) = let
    elxsc = elxs[c]
    for _i in axes(ex, 2)
        ex[1, _i] = elxsc[j, _i]; 
        ex[2, _i] = elxsc[k, _i]; 
    end
    ex
end
@time @views let
    c = 4
    ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
end
c = 4
@time copy2ex!(ex, elxs, c, 1, 2)
@time copy2ex!(ex, elxs, c, 1, 2)
@time copy2ex!(ex, elxs, c, 1, 2)
end
end

```

gives

```julia
WARNING: replacing module mbas001.                                            
  0.000013 seconds (15 allocations: 512 bytes)                                
  0.020667 seconds (6.54 k allocations: 338.026 KiB, 99.88% compilation time) 
  0.000005 seconds (9 allocations: 352 bytes)                                 
  0.000003 seconds (9 allocations: 352 bytes)                                 
Main.mbas001 

```

Edit: Note that I forgot to remove the `views`. Everything is local now.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 6, 2022, 6:00pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/8 "2022-08-06T18:00:57Z")

</div>

Doesn’t `@time` itself allocate? I always use `@btime` and variable interpolation for reliable allocation estimates.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 6:02pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/9 "2022-08-06T18:02:17Z")

</div>

It actually worked (sort of) in [https://discourse.julialang.org/t/why-is-there-an-allocation/85405/4:](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/4:) there were zero allocations with the function.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 6, 2022, 6:05pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/10 "2022-08-06T18:05:37Z")

</div>

`@btime` seems to have improved over time, but it might still be an idea to use BenchmarkTools to be more certain.

---

<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 6, 2022, 6:28pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/11 "2022-08-06T18:28:51Z")

</div>

> [@PetrKryslUCSD](#):
>
> …

please implement all the changes I proposed:

```julia
julia> module mbas001
       const elxs = fill(Matrix{Float64}(undef, 3, 3), 7)
       for _i in eachindex(elxs)
           elxs[_i] = rand(3, 3)
       end
       const ex = fill(zero(Float64), 3, 3) 
       copy2ex!(ex, elxs, c, j, k) = let
           elxsc = elxs[c]
           for _i in axes(ex, 2)
               ex[1, _i] = elxsc[j, _i]; 
               ex[2, _i] = elxsc[k, _i]; 
           end
           ex
       end
       @time @views let
           c = 4
           ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
       end
       c = 4
       @time copy2ex!(ex, elxs, c, 1, 2)
       @time copy2ex!(ex, elxs, c, 1, 2)
       @time copy2ex!(ex, elxs, c, 1, 2)
       end
  0.000001 seconds
  0.012696 seconds (7.42 k allocations: 388.971 KiB, 99.93% compilation time)
  0.000003 seconds
  0.000002 seconds
Main.mbas001

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 6:30pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/12 "2022-08-06T18:30:18Z")

</div>

Thank you, I appreciate your suggestion, but really I want to have only local variables.  
Testing in the global scope was obviously a mistake.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 6, 2022, 7:00pm UTC](https://discourse.julialang.org/t/why-is-there-an-allocation/85405/13 "2022-08-06T19:00:30Z")

</div>

It is possible that `@time` allocates when it is used in a local scope. Tracking allocations  
with everything in local scope:

```julia
        - module mbas001
        - let
        - elxs = fill(Matrix{Float64}(undef, 3, 3), 7)
        - for _i in eachindex(elxs)
        - elxs[_i] = rand(3, 3)
        - end
        - ex = fill(zero(Float64), 3, 3) 
        - copy2ex!(ex, elxs, c, j, k) = let
        0 elxsc = elxs[c]
        0 for _i in axes(ex, 2)
        0 ex[1, _i] = elxsc[j, _i]; 
        0 ex[2, _i] = elxsc[k, _i]; 
        0 end
        0 ex
        - end
        - let
        - c = 4
        - ex[1,:] = elxs[c][1,:]; ex[2,:] = elxs[c][2,:]; 
        - end
        - c = 4
        - copy2ex!(ex, elxs, c, 1, 2)
        - copy2ex!(ex, elxs, c, 1, 2)
        - copy2ex!(ex, elxs, c, 1, 2)
        - end
        - end

```
