Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation and interpolation memory leaks #385

Merged
merged 10 commits into from
Jan 7, 2025

Conversation

willow-ahrens
Copy link
Collaborator

Fixes #339. Fixes a memory leak in interpolated variables from setup and teardown, continuing what was started in #265. Additionally, adds a cache of already-compiled sampler functions, so that wrapping a @benchmark in a for-loop doesn't compile separate functions each iteration.

Here's an example:

Setup

julia> using BenchmarkTools, Printf

julia> function f(n)
           x = rand(Int, n)
           target = sort(x)
           y = copy(target)
           @belapsed sort!($y) setup=($y == $target || error("Bad sort"); copyto!($y, $x)) evals=1 gctrial=false samples=3
       end
f (generic function with 1 method)

julia> function meminfo_julia()
           GC.gc()
           @printf "GC total:  %9.3f MiB\n" Base.gc_total_bytes(Base.gc_num())/2^20
           @printf "GC live:   %9.3f MiB\n" Base.gc_live_bytes()/2^20
           @printf "JIT:       %9.3f MiB\n" Base.jit_total_bytes()/2^20
           @printf "Max. RSS:  %9.3f MiB\n" Sys.maxrss()/2^20
       end

Main Branch

julia> meminfo_julia()
GC total:    121.857 MiB
GC live:      14.555 MiB
JIT:           0.384 MiB
Max. RSS:    338.000 MiB

julia> times = f.(1594323:1594323+100);

julia> meminfo_julia()
GC total:  10147.292 MiB
GC live:    3695.383 MiB
JIT:           1.550 MiB
Max. RSS:   4152.938 MiB

After Fixing Setup Leaks

julia> meminfo_julia()
GC total:     77.615 MiB
GC live:      14.343 MiB
JIT:           0.165 MiB
Max. RSS:    305.688 MiB

julia> times = f.(1594323:1594323+100);

julia> meminfo_julia()
GC total:  10112.637 MiB
GC live:       9.663 MiB
JIT:           1.326 MiB
Max. RSS:    495.234 MiB

After Fixing Redundant Compilation

julia> meminfo_julia()
GC total:     77.695 MiB
GC live:      14.409 MiB
JIT:           0.165 MiB
Max. RSS:    306.406 MiB

julia> times = f.(1594323:1594323+100);

julia> meminfo_julia()
GC total:  10011.679 MiB
GC live:       8.914 MiB
JIT:           0.416 MiB
Max. RSS:    476.047 MiB

Copy link
Member

@vchuravy vchuravy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, but I must admit I don't understand this code anymore.

@willow-ahrens willow-ahrens merged commit 20fd596 into JuliaCI:main Jan 7, 2025
9 checks passed
@willow-ahrens
Copy link
Collaborator Author

It's confusing to need the function to always run at global scope, and requires some involved metaprogramming to get that right. Hopefully this is one step closer to a correct approach which is still backwards-compatible with the older versions.

@LilithHafner
Copy link
Contributor

This is remarkably clever. Brava!

LilithHafner added a commit to LilithHafner/Chairmarks.jl that referenced this pull request Feb 15, 2025
Because @willow-ahrens fixed this leak in a non-breaking release of BenchmarkTools so it was apparently not unavoidable. (JuliaCI/BenchmarkTools.jl#385)
LilithHafner added a commit to LilithHafner/Chairmarks.jl that referenced this pull request Feb 15, 2025
Because @willow-ahrens fixed this leak in a non-breaking release of BenchmarkTools so it was apparently not unavoidable. (JuliaCI/BenchmarkTools.jl#385)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak when repeatedly benchmarking
3 participants