# Using \`@allocated\` to track memory allocations

**URL:** <https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617>\
**Category:** General Usage\
**Created:** [July 3, 2017, 1:32pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617 "2017-07-03T13:32:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 3, 2017, 1:32pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/1 "2017-07-03T13:32:30Z")

</div>

I am having a hard time understanding allocation in some code I have. The minimal example is the code below:

```julia
function foo(c)
    v = c * 1
    v
end

c = 1.0

@show @allocated foo(c)
@show @allocated foo(c)
@show @allocated foo(c)

```

I would expect this code not to incur in any allocation. However, the result is

```julia
@allocated(foo(c)) = 16
@allocated(foo(c)) = 16
@allocated(foo(c)) = 16

```

Why is my understanding not correct?

Thanks!

---

<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:** [July 3, 2017, 1:51pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/2 "2017-07-03T13:51:01Z")

</div>

It’s just because you’re working in global scope. Try putting the call to `@allocated` inside a function to get a more representative value.

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 3, 2017, 1:53pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/3 "2017-07-03T13:53:12Z")

</div>

What part is in global scope? does not `foo(c)` run entirely in its own scope?

---

<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:** [July 3, 2017, 2:00pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/4 "2017-07-03T14:00:39Z")

</div>

If you’re working at the REPL, then `c` itself is a global variable. It can’t have a concrete inferred type because you could reassign it to anything. But once you’re inside a function, even when called from the REPL, everything is fine. This is the same reason that we always tell people not to benchmark in global scope from there REPL.

Since manually wrapping things in a function can be annoying, I wrote a `@wrappedalllcs` macro that tries to do it for you. You might find this helpful: [https://github.com/rdeits/TypedPolynomials.jl/blob/master/test/runtests.jl](https://github.com/rdeits/TypedPolynomials.jl/blob/master/test/runtests.jl)

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 3, 2017, 2:03pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/5 "2017-07-03T14:03:57Z")

</div>

OK, thanks. I just fail to see why `foo(c)` would make reference to variables in the global scope.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [July 3, 2017, 2:06pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/6 "2017-07-03T14:06:56Z")

</div>

Because the function call accesses `c` – it’s an input after all. You can also declare that as `const` and get no allocations:

```julia
julia> const d = 1.0
1.0

julia> @show @allocated foo(d)
@allocated(foo(d)) = 0
0

```

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 3, 2017, 2:09pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/7 "2017-07-03T14:09:51Z")

</div>

Thanks.

My (wrong) understanding was that `foo(c)` would be a self-contained piece of code that made no reference to the global variable, as if the current value of `c` had been read somewhere and given as an input for `foo` to be executed.

---

<div class="post-metadata">

**Author:** ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)\
**Post date:** [July 3, 2017, 3:45pm UTC](https://discourse.julialang.org/t/using-allocated-to-track-memory-allocations/4617/8 "2017-07-03T15:45:37Z")

</div>

Just to add a last comment to this discussion. Wrapping the call to the macro in a function yields no allocations, as expected.

```julia
julia> bar(c) = @allocated foo(c)
julia> @show bar(c)
bar(c) = 0
0

```
