# Memory Allocation in \`@time\` but not in \`AllocCheck.check\_allocs\`

**URL:** <https://discourse.julialang.org/t/memory-allocation-in-time-but-not-in-alloccheck-check-allocs/124638>\
**Category:** Performance\
**Tags:** question\
**Created:** [January 10, 2025, 11:48am UTC](https://discourse.julialang.org/t/memory-allocation-in-time-but-not-in-alloccheck-check-allocs/124638 "2025-01-10T11:48:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 10, 2025, 11:48am UTC](https://discourse.julialang.org/t/memory-allocation-in-time-but-not-in-alloccheck-check-allocs/124638/1 "2025-01-10T11:48:34Z")

</div>

Hi there, I am wondering what this is about:

In `@time` I see an allocation of 16 Bytes.  
When using `AllocCheck.check_allocs`, everything is clean.

Here a code example:

```julia
using Statistics
using LinearAlgebra
using StaticArrays
using AllocCheck

xs = 1:0.1:30
ys = sin.(xs) + 0.1xs + rand(length(xs))

sumofsquares(n) = n*(n+1.0)*(2.0*n+1.0)/6.0
smooth(a, b, γ) = a + γ * (b - a)

function mytest(ys)
    # simplified implementation of https://stats.stackexchange.com/a/370175
    # x is the diagonal offset, y the percentage of local recurrence
    # we compute the slope of a simple linear regression with bias
    xs = 1:length(ys)
    x_mean = mean(xs)
    xx_mean = sumofsquares(length(xs)) / length(xs)

    n = 0.0
    y_mean = 0.0
    xy_mean = 0.0
    for (x, y) in zip(xs, ys)
        n += 1.0
        y_mean = smooth(y_mean, y, inv(n))
        xy_mean = smooth(xy_mean, x*y, inv(n))
    end
    A = SA[ 
        xx_mean x_mean
        x_mean 1.0
    ]
    b = SA[xy_mean, y_mean]
    return (A \ b)[1] # slope
end

@time mytest(ys) # 0.000008 seconds (1 allocation: 16 bytes)
AllocCheck.check_allocs(mytest, Tuple{Vector{Float64}}) # Any[]

```

(`@profview_allocs` seems to follow `check_allocs` from the second execution onwards. I saw once an allocation in `@profview_allocs` as a `Vector{Any}` within `IncrementalCompact` `InstructionStream` from the `ir.jl` file, but not sure about the precise mechanics which produced this)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 10, 2025, 3:03pm UTC](https://discourse.julialang.org/t/memory-allocation-in-time-but-not-in-alloccheck-check-allocs/124638/2 "2025-01-10T15:03:32Z")

</div>

It’s not runnable for me because `smooth` is missing, but this looks like compilation of a runtime dispatch with global variables, which AllocCheck skips because it’s not happening within the method call itself. The Performance Tips mention you can put the `@time` call inside a timing method to get rid of the allocation, but you could also make a `const` global like `const ys2 = ys; @time mytest(ys2)` or interpolate global variables into `@btime mytest($ys2)` if you have the extra seconds for a more robust benchmark. I’m not familiar with Chairmarks.jl, but it also supports interpolation and it seems like `@b ys2 mytest` accomplishes the same thing as interpolation.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 10, 2025, 3:28pm UTC](https://discourse.julialang.org/t/memory-allocation-in-time-but-not-in-alloccheck-check-allocs/124638/3 "2025-01-10T15:28:43Z")

</div>

thank you so much! indeed the single allocation goes away as soon as I interpolate the global with benchmarktools ❤  
(I also added the `smooth` function now)
