How to count heap allocations easily?

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

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:

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

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?

1 Like

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

this is a non-constant global,

trust this number

1 Like

To be clear, @allocated only returns heap allocations.

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

3 Likes

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

Thanks!