# Memory allocations are not very understandable

**URL:** <https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696>\
**Category:** Performance\
**Created:** [December 11, 2017, 9:25pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696 "2017-12-11T21:25:15Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![rvlander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvlander/32/2863_2.png) [@rvlander](https://discourse.julialang.org/u/rvlander)\
**Post date:** [December 12, 2017, 3:16pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/21 "2017-12-12T15:16:33Z")

</div>

I tried adding a variable to collect values returned by `winsMilestone` but `.mem` file still is the same.

---

<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 12, 2017, 3:17pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/22 "2017-12-12T15:17:53Z")

</div>

Well if `@time` says there are no allocations it probably all got elided.

---

<div class="post-metadata">

**Author:** ![rvlander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvlander/32/2863_2.png) [@rvlander](https://discourse.julialang.org/u/rvlander)\
**Post date:** [December 12, 2017, 3:24pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/23 "2017-12-12T15:24:22Z")

</div>

@time says there were 6 allocations of 320 bytes. This is why this seems strange.

---

<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 12, 2017, 3:26pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/24 "2017-12-12T15:26:36Z")

</div>

When you put it in a function it didn’t show any allocations.

For me it shows:

```julia
f() = @time test(1000)

julia> @time test(1000)
  0.000013 seconds (5 allocations: 304 bytes)

julia> f()
  0.000013 seconds (1 allocation: 144 bytes)

```

which I guess is the allocation of the array.

---

<div class="post-metadata">

**Author:** ![rvlander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvlander/32/2863_2.png) [@rvlander](https://discourse.julialang.org/u/rvlander)\
**Post date:** [December 12, 2017, 3:32pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/25 "2017-12-12T15:32:18Z")

</div>

I just run this code:

```julia
using Profile

@inline function winsMilestone(topSide, bottomSide)
    a = 0

    if (count(!iszero, topSide) < 3 || count(!iszero, bottomSide) < 3)
         return 1
    end

    return 2
end

function test(n_iter)
    A = zeros(UInt8,(7, 9))
    sum = 0
    for i=1:n_iter
        for j=1:9
            sum += @views winsMilestone(A[1:3, j], A[4:7, j])
        end
    end
    return sum
end

@time test(1)

Profile.clear_malloc_data()
@time test(1000)

```

In console:

```julia
> julia --track-allocation=user alloc-test.jl
  0.117380 seconds (135.18 k allocations: 7.587 MiB)
  0.016712 seconds (6 allocations: 320 bytes)

```

It generates the following mem:

```julia
        - using Profile
        - 
        - @inline function winsMilestone(topSide, bottomSide)
        - a = 0
        - 
        0 if (count(!iszero, topSide) < 3 || count(!iszero, bottomSide) < 3)
        0 return 1
        - end
        - 
        0 return 2
        - end
        - 
        - function test(n_iter)
        0 A = zeros(UInt8,(7, 9))
        0 sum = 0
        0 for i=1:n_iter
        0 for j=1:9
        0 sum += @views winsMilestone(A[1:3, j], A[4:7, j])
        - end
        - end
        0 return sum
        - end
        - 
        - @time test(1)
        - 
        - Profile.clear_malloc_data()
        - @time test(1000)

```

As you can see, the reported figures do not match.

---

<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 12, 2017, 3:37pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/26 "2017-12-12T15:37:06Z")

</div>

And I have told you why.

> [@kristoffer.carlsson](#):
>
> I think these 5 allocations are from the the stuff that @time expands to, which will be in global scope.

and also to put the `@time` call in a function to avoid this.

```julia
julia> @macroexpand @time time(1000)
quote
    #= util.jl:167 =#
    local #21#stats = (Base.gc_num)()
    #= util.jl:168 =#
    local #23#elapsedtime = (Base.time_ns)()
    #= util.jl:169 =#
    local #22#val = time(1000)
    #= util.jl:170 =#
    #23#elapsedtime = (Base.time_ns)() - #23#elapsedtime
    #= util.jl:171 =#
    local #24#diff = (Base.GC_Diff)((Base.gc_num)(), #21#stats)
    #= util.jl:172 =#
    (Base.time_print)(#23#elapsedtime, #24#diff.allocd, #24#diff.total_time, (Base.gc_alloc_count)(#24#diff))
    #= util.jl:174 =#
    #22#val
end

```

These variables are in global scope and will likely cause allocations.

---

<div class="post-metadata">

**Author:** ![rvlander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvlander/32/2863_2.png) [@rvlander](https://discourse.julialang.org/u/rvlander)\
**Post date:** [December 12, 2017, 4:34pm UTC](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696/27 "2017-12-12T16:34:28Z")

</div>

Thanks. I guess we can say the initial question has been solved.

[Previous page](https://discourse.julialang.org/t/memory-allocations-are-not-very-understandable/7696.md?page=1)
