# What does allocation mean when creating a new array

**URL:** <https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122>\
**Category:** New to Julia\
**Created:** [December 30, 2018, 5:28pm UTC](https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122 "2018-12-30T17:28:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [December 30, 2018, 5:28pm UTC](https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122/1 "2018-12-30T17:28:03Z")

</div>

Consider this basic function

```julia
function allocation_test()
    A = Vector{Float64}(undef, 15)
    return A
end

```

I have a few questions about the memory usage of this function.

```julia
julia> @time allocation_test();
  0.000003 seconds (5 allocations: 368 bytes)

julia> @btime allocation_test();
  34.844 ns (1 allocation: 208 bytes)

```

1. Why the apparent difference in `@time` and `@btime` macros?
2. What is the 368 bytes (and 208 bytes) measuring? I’ve created an array of 15 `Float64` elements all taking 8 bytes at most. So shouldn’t the allocation be 8\*15 = 120 bytes? Even if one accounts for “overhead” the 368 bytes is more than twice that number.
3. Is this what it means by heap allocated ? If this was stack allocated, it would show as “0 allocations” right?
4. If I now assign the function to a variable like:

```julia
julia> @time b=allocation_test();
  0.000005 seconds (6 allocations: 416 bytes)

```

why is there extra memory usage? Should `b` just point to the array already created on the heap?

Thanks,

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 30, 2018, 6:28pm UTC](https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122/2 "2018-12-30T18:28:03Z")

</div>

1. Put things into functions if you want accurate allocation measurements (or like you saw, use BenchmarkTols):

```julia
julia> function allocation_test()
           A = Vector{Float64}(undef, 15)
           return A
       end
allocation_test (generic function with 1 method)

julia> f() = @time allocation_test()
f (generic function with 1 method)

julia> f()
  0.000001 seconds (1 allocation: 208 bytes)

```

1. There is some overhead in allocating a julia array over just the content ([julia/julia.h at 2e91c5e73af4e44c45e942e057e5c09fa062cf8f · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/2e91c5e73af4e44c45e942e057e5c09fa062cf8f/src/julia.h#L166-L185))

2. Yes.

3. Because you are using more global variables, see 1.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [December 30, 2018, 6:39pm UTC](https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122/3 "2018-12-30T18:39:05Z")

</div>

I don’t understand the point 1. I am calling a function. Are you saying that even if `@time` is run from a global scope, there are issues? So the allocations I saw were allocations `@time` was using for its own purpose?

I am having a hard time profiling my code because I don’t know how many “functions” to nest before calling `@time`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 30, 2018, 6:51pm UTC](https://discourse.julialang.org/t/what-does-allocation-mean-when-creating-a-new-array/19122/4 "2018-12-30T18:51:44Z")

</div>

Global variables typically cause allocations. The `@time` macro expands to many variables, so if `@time` is called in global scope there will be a few extra allocations coming from the macro itself.

```julia
julia> @macroexpand @time a = 1+1
quote
    local #9#stats = (Base.gc_num)()
    local #11#elapsedtime = (Base.time_ns)()
    local #10#val = (a = 1 + 1)
    #11#elapsedtime = (Base.time_ns)() - #11#elapsedtime
    local #12#diff = (Base.GC_Diff)((Base.gc_num)(), #9#stats)
    (Base.time_print)(#11#elapsedtime, (#12#diff).allocd, (#12#diff).total_time, (Base.gc_alloc_count)(#12#diff))
    (Base.println)()
    #10#val
end

```

This is described in the performance tips section in the manual ([Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Measure-performance-with-%5B@time%5D(@ref)-and-pay-attention-to-memory-allocation-1)):

> The 5 allocations seen are from running the `@time` macro itself in global scope. If we instead run the timing in a function, we can see that indeed no allocations are performed:

> [@affans](#):
>
> I am having a hard time profiling my code because I don’t know how many “functions” to nest before calling `@time` .

Either use BenchmarkTools or make sure `@time` is not called from global scope. The overhead will be in the hundreds of bytes so for most measurements it doesn’t matter.
