# How do I determine the memory allocations in Julia using @code\_typed (or @code\_llvm)

**URL:** <https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510>\
**Category:** General Usage\
**Created:** [September 4, 2018, 12:28am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510 "2018-09-04T00:28:34Z")\
**Posts on this page:** 5\
**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:** [September 4, 2018, 12:28am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510/1 "2018-09-04T00:28:34Z")

</div>

I am looking for a basic understanding of how allocations “work” in Julia.

See this code (obtained from another post I was reading).

```julia
 function sum_of_diff_1(x)
         sum(diff(x))
 end

```

A vectorized version of the sum of differences. Here is the same code using loops

Running

```julia
 @time sum_of_diff_1(rand(100))
  0.000007 seconds (9 allocations: 1.969 KiB)

```

So we see 9 allocations. Could someone explain to me where these 9 allocations are in @code\_typed (or @code\_warntype, @code\_llvm) and exactly what it means?

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:** [September 4, 2018, 1:01am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510/2 "2018-09-04T01:01:34Z")

</div>

`@time` defines some variables which will here be in global scope and will thus be boxed (and allocated on the heap).

If you out the `@time` macro in a function, some of the allocations would go away.

---

<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:** [September 4, 2018, 1:19am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510/4 "2018-09-04T01:19:41Z")

</div>

Sorry, I should be more clear. I am not asking why the allocations are happening, but “where” its happening in the compiled code. i.e. what are the commands in @code\_typed or @code\_llvm that allocate memory. I guess the example I have is a bad one but I don’t know of a good example.

I suppose I am asking someone to teach me how to read llvm and I am not at that level yet so I can revisit this question in a few months/years 😛

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 4, 2018, 1:39am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510/5 "2018-09-04T01:39:03Z")

</div>

I don’t think you’ll see the allocations in that particular case. The reason is that `@code_typed(sum(rand(100)))` does, basically:

1. Compute `rand(100)` (this allocates an array)
2. Look up the type of the result, call it `T`
3. Look up the appropriate specialized method for `sum(::T)` for that type `T`.

So the allocation (which happened inside the call to `rand(100)`) isn’t part of what `@code_typed` is showing you.

On the other hand, you can look at:

```julia
julia> @code_typed rand(100)
CodeInfo(
243 1 ── %1 = (Base.getfield)(dims, 1, true)::Int64 │╻╷╷ Type
    │ %2 = Random.GLOBAL_RNG::MersenneTwister │╻ rand
    │ %3 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Float64,1}, svec(Any, Int64), :(:ccall), 2, Array{Float64,1}, :(%1), :(%1)))::Array{Float64,1} ││╻╷╷ rand

```

which does show the `jl_alloc_array_1d` call. However, `@code_whatever` is only going to show you the given function and whatever was inlined into it. If the allocation happens to occur inside some function which was not inlined (which is very often the case), then the `@code_whatever` tools won’t necessarily show it:

```julia
julia> @noinline f() = rand(100)
f (generic function with 1 method)

julia> g() = f()
g (generic function with 1 method)

julia> @code_typed(g())
CodeInfo(
1 1 ─ %1 = invoke Main.f()::Array{Float64,1} │
  └── return %1 │
) => Array{Float64,1}

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 4, 2018, 7:33am UTC](https://discourse.julialang.org/t/how-do-i-determine-the-memory-allocations-in-julia-using-code-typed-or-code-llvm/14510/6 "2018-09-04T07:33:08Z")

</div>

As other people have already explained, at least some of those allocations happen in the `@time` macro and the `rand(100)` construction. The easiest way to get rid of those distractions is to use BenchmarkTools and precompute the input to the function.

```julia
julia> using BenchmarkTools

julia> function sum_of_diff_1_loop(x)
           s = zero(eltype(x))
           for k = 2:length(x)
               s += x[k] - x[k - 1]
           end
           return s
       end
sum_of_diff_1_loop (generic function with 1 method)

julia> x = rand(100);

julia> @btime sum_of_diff_1_loop($x);
  80.965 ns (0 allocations: 0 bytes)

```

If this says 0 allocations you can stop looking for allocations.

As for the `@code_whatever` tools, if `@code_warntype` marks things red, frequently allocations will be needed to handle that (with the exception of some small unions in Julia 0.7 and up). In `@code_llvm` I only know to look for `alloca`, but allocations can easily be hidden in calls to non-inlined functions.

But this is relatively easy to experiment with. Write the smallest function you can that does some kind of allocation, and as little else as possible, and see what shows up in `@code_whatever`. If necessary, compare to what turns up in non-allocating small functions.
