Hehe, this has made me curious…
[…] if you avoid allocations (i.e. the GC) then you don’t need to turn the GC off. Because the GC is only triggered on allocations. […]
…cause it is not so, in java, always, but I don’t know enough about julia’s inner workings.
So I started with a very trivial example of a recursive function, to be extended, until finally, I would include some code, which would require a heap-allocation (for checking when, and how often the gc would be called). But I failed to realize, that even prior to any extension, just calling @btime
would already result in the gc being called (as is the case for using and precompiling any packages).
It’s a fun example, of just how much a gc does, anyways, though (which we often don’t realize)…
julia> GC.enable_logging(true)
julia> function GC.gc(full::Bool=true)
println("==> gc was called with full= ",full)
ccall(:jl_gc_collect, Cvoid, (Cint,), full ? 1 : 2)
end
julia> f(x) = x<=1 ? x : f(x-1) + 1
…this is the output, that is generated…
julia> @btime f(10)
==> gc was called with full= true
GC: pause 57.34ms. collected 10.118183MB. full recollect
GC: pause 252.49ms. collected 0.023080MB. incr
==> gc was called with full= true
GC: pause 49.95ms. collected 0.000832MB. full recollect
GC: pause 250.39ms. collected 0.000000MB. incr
==> gc was called with full= true
GC: pause 45.77ms. collected 0.000160MB. full recollect
GC: pause 245.63ms. collected 0.000000MB. incr
==> gc was called with full= true
GC: pause 46.93ms. collected 0.000160MB. full recollect
GC: pause 239.19ms. collected 0.000000MB. incr
==> gc was called with full= true
GC: pause 45.38ms. collected 0.080160MB. full recollect
GC: pause 243.06ms. collected 0.008192MB. incr
==> gc was called with full= true
GC: pause 46.80ms. collected 0.000160MB. full recollect
GC: pause 249.88ms. collected 0.000000MB. incr
==> gc was called with full= true
GC: pause 44.94ms. collected 0.000160MB. full recollect
GC: pause 236.46ms. collected 0.000000MB. incr
==> gc was called with full= true
GC: pause 47.28ms. collected 0.000160MB. full recollect
GC: pause 240.18ms. collected 0.000304MB. incr
13.828 ns (0 allocations: 0 bytes)
10
Not surprising, to those, who know the inner workings of julia well enough, probably. And not to me, after thinking about it, a bit, but it was, after invoking @btime f(10)
for the first time.