# Reduce memory allocation in copying array

**URL:** <https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536>\
**Category:** New to Julia\
**Tags:** question, memory-allocation\
**Created:** [August 17, 2021, 8:09am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536 "2021-08-17T08:09:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frisus95](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Frisus95](https://discourse.julialang.org/u/Frisus95)\
**Post date:** [August 17, 2021, 8:09am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/1 "2021-08-17T08:09:06Z")

</div>

Hello everyone.

I was wondering if there is a way to reduce the memory allocation of a code like this:

```julia
julia> function funza(N)
         a = Array{Float64}(undef,5)
         b = Array{Float64}(undef,5)
             for i=1:N
             b = rand(5)
             a = b
             # println(a)
             # println(b)
             end
         end
funza (generic function with 1 method)

```

Which gives:

```julia
julia> @time funza(10^6)
  0.146703 seconds (1.00 M allocations: 122.071 MiB, 15.50% gc time)

```

There is essentially an allocation every time the row:

b = rand(5)

is executed.

Even if I don’t really think it’s possible, is there any way to reduce the memory allocation of such a code?

Thanks a lot to everyone.

---

<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 17, 2021, 8:26am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/2 "2021-08-17T08:26:26Z")

</div>

> [@Frisus95](#):
>
> `b = rand(5)`

this allocates `rand(5)` then bind `b` to it. Do this instead:

```julia

julia> using Random

julia> @benchmark (Random.rand!(b)) setup=(b=zeros(5))
BenchmarkTools.Trial: 10000 samples with 996 evaluations.
 Range (min … max): 23.196 ns … 502.040 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 24.087 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 24.902 ns ± 5.406 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▂▂▅█▆ ▂▃▃▂ ▃▂ ▁ ▂▁ ▂ ▁▁ ▂
  ██████▇▇████▇▇█▆▅██▆▆█▅▅██▆▁██▁██▆▁▃▄▅▁▁▁▅▃▁▁▄▅▁▃▄▄▄▁▁▁▁▁▁▁▃ █
  23.2 ns Histogram: log(frequency) by time 37.6 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

---

<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 17, 2021, 8:31am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/3 "2021-08-17T08:31:59Z")

</div>

> [@Frisus95](#):
>
> ```julia
> julia> function funza(N)
> a = Array{Float64}(undef,5)
> b = Array{Float64}(undef,5)
> for i=1:N
> b = rand(5)
> a = b
> # println(a)
> # println(b)
> end
> end
> 
> ```

It’s not easy to say exactly how much you can reduce allocations. Your function doesn’t return anything, so it can be replaced with

```julia
function funza(N)
    return nothing
end

```

which has zero allocations. What should the function return? If you can say that, then it’s possible to make recommendations.

---

<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 17, 2021, 8:39am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/4 "2021-08-17T08:39:33Z")

</div>

I’ll try to interpret your code. Perhaps you want the function to print some random numbers? Then you can do

```julia
function funza(N)
     b = Array{Float64}(undef,5)
     for i in 1:N
         b .= rand.() # this updates b in-place, and does not allocate anything
         a = b # this does not allocate, it just puts the label a on the same vector as b
         println(a)
         println(b)
     end
end

```

Now, `println` itself is going to create most of the allocations, there will only be a single allocation of a `Float64` vector.

---

<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 17, 2021, 8:42am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/5 "2021-08-17T08:42:23Z")

</div>

ah yes…always forget about dot fusion… too magical

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [August 17, 2021, 8:45am UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/6 "2021-08-17T08:45:17Z")

</div>

You can also do:

```julia
using Random

function funza(N)
    a = Array{Float64}(undef,5)
    b = Array{Float64}(undef,5)
    for i=1:N
        rand!(b)
        a .= b
        # println(a)
        # println(b)
    end
end

julia> @time funza(1e6)
  0.022101 seconds (2 allocations: 256 bytes)

```

---

<div class="post-metadata">

**Author:** ![Frisus95](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Frisus95](https://discourse.julialang.org/u/Frisus95)\
**Post date:** [August 17, 2021, 1:04pm UTC](https://discourse.julialang.org/t/reduce-memory-allocation-in-copying-array/66536/7 "2021-08-17T13:04:12Z")

</div>

Thanks to all of you who responded. Your answers have all been great, but it’s my fault: I posted too simple code that doesn’t reflect what I’m trying to do (posting the real code here would be far too complex).

I will close the topic, and maybe I will open another one in the future in case of need.

Thanks again to everyone and have a nice day!
