Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lorenzoh/FluxTraining.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzoh committed Jan 18, 2021
2 parents e4fc3d9 + bd74557 commit a34b58b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/callbacks/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,27 @@ function on(::BatchBegin, ::Phase, cb::ToGPU, learner)
end


garbagecollect() = (GC.gc(); ccall(:malloc_trim, Cvoid, (Cint,), 0))
function garbagecollect()
GC.gc()
if Base.Sys.islinux()
ccall(:malloc_trim, Cvoid, (Cint,), 0)
end
end


"""
GarbageCollect(nsteps)
Every `nsteps` steps, forces garbage collection.
Use this if you get memory leaks from, for example, parallel data loading.
Use this if you get memory leaks from, for example,
parallel data loading.
Performs an additional C-call on Linux systems that can
sometimes help.
"""
function GarbageCollect(nsteps::Int = 100)
return throttle(CustomCallback((learner) -> garbagecollect(), BatchEnd), freq = nsteps)
return throttle(
CustomCallback((learner) -> garbagecollect(), BatchEnd, Phase),
BatchEnd(),
freq = nsteps)
end
8 changes: 8 additions & 0 deletions test/callbacks/garbagecollect.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

include("../imports.jl")

@testset ExtendedTestSet "`GarbageCollect`" begin
cb = GarbageCollect()
learner = testlearner(Recorder(), Metrics(), cb)
@test_nowarn fit!(learner, 1)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ include("./imports.jl")
include("./callbacks/recorder.jl")
include("./callbacks/scheduler.jl")
include("./callbacks/checkpointer.jl")
include("./callbacks/garbagecollect.jl")
include("./callbacks/sanitycheck.jl")
end

0 comments on commit a34b58b

Please sign in to comment.