# How to count heap allocations easily?

**URL:** <https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285>\
**Category:** Performance\
**Tags:** question, memory-allocation\
**Created:** [September 16, 2021, 8:44pm UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285 "2021-09-16T20:44:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)\
**Post date:** [September 16, 2021, 8:44pm UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285/1 "2021-09-16T20:44:03Z")

</div>

The manual recommends using `@time` and `@allocated` to examine memory use:

[https://docs.julialang.org/en/v1/manual/profile/#Memory-allocation-analysis](https://docs.julialang.org/en/v1/manual/profile/#Memory-allocation-analysis)

It appears that both of those macros measure both stack and heap allocations, and from a performance point of view I would prefer to focus on just heap allocations. In fact the `@btime` macro in BenchmarkTools seems to report just heap allocations, but it is quite a complicated macro and I would like something with the simplicity of `@allocated` akin to “`@heapallocated`” that would just return the number of heap allocated bytes.

Focusing on the heap seems especially relevant since v1.5 where `@view` became a stack allocation:

> **[Julia 1.5 Highlights](https://julialang.org/blog/2020/08/julia-1.5-highlights/#struct_layout_and_allocation_optimizations)**
>
> Julia version 1.5 has been released, featuring many performance improvements and new capabilities.

Here is an MWE of the kind of code where I would like to measure heap allocations (to make sure they are zero):

```julia
using BenchmarkTools

function f1(x)
    v = @view x[:,2] # this should allocate on stack, not heap
    v[1]
end

x = zeros(2^20,2^3)
@show @allocated f1(x) # large the first time - compiling
@show @allocated f1(x) # 16 bytes - for the "@view" (on stack)
@show @ballocated f1(x) # 16 again
@time f1(x) # 16 again
@btime f1($x) # 0 bytes !

```

Clearly the capability is there in `@btime` but it comes along with a lot of other stuff (and runs longer) so there must be a simpler way?

---

<div class="post-metadata">

**Author:** ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)\
**Post date:** [June 1, 2022, 1:21am UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285/2 "2022-06-01T01:21:10Z")

</div>

Perhaps the new `Profile.Allocs` in 1.8 will be helpful:

> <https://github.com/JuliaLang/julia/blob/v1.8.0-rc1/NEWS.md#profile>

---

<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:** [June 1, 2022, 4:17am UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285/3 "2022-06-01T04:17:36Z")

</div>

> [@JeffFessler](#):
>
> `x = zeros(2^20,2^3)`

this is a non-constant global,

> [@JeffFessler](#):
>
> `@btime f1($x) # 0 bytes !`

trust this number

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [June 1, 2022, 7:06am UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285/4 "2022-06-01T07:06:31Z")

</div>

To be clear, `@allocated` only returns heap allocations.

But dynamic dispatches can allocate, i.e. dispatches on `x`.

---

<div class="post-metadata">

**Author:** ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)\
**Post date:** [June 1, 2022, 10:31pm UTC](https://discourse.julialang.org/t/how-to-count-heap-allocations-easily/68285/5 "2022-06-01T22:31:52Z")

</div>

Based on your feedback, it looks like what I need is simply  
`@ballocated f1($x)`

Thanks!
