Skip to content

Commit cc08854

Browse files
committed
feat(worker): add requestgc api
1 parent 124c471 commit cc08854

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Malt.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,18 @@ function interrupt(w::InProcessWorker)
632632
nothing
633633
end
634634

635+
"""
636+
Malt.requestgc(w::Worker)
635637
638+
Request a garbage collection on the worker `w`. This is a non-blocking call (on the worker).
639+
"""
640+
function requestgc(w::Worker)
641+
if !isrunning(w)
642+
@warn "Tried to gc a worker that has already shut down." summary(w)
643+
else
644+
remote_eval_wait(Main, worker, :(Base.notify(Main._gc_event)))
645+
end
646+
end
636647

637648

638649
# Based on `Base.task_done_hook`

src/worker.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ format_error(err, bt) = sprint() do io
161161
end
162162

163163
const _channel_cache = Dict{UInt64, AbstractChannel}()
164+
const _gc_event = Threads.Event(true)
165+
166+
const _gc_task = Threads.@spawn :default begin
167+
for _i in Iterators.countfrom(1)
168+
wait(_gc_event)
169+
sleep(5) # debounce 5 seconds
170+
GC.gc(true)
171+
# ignore all events after the gc
172+
@atomic _gc_event.set = false
173+
end
174+
end
164175

165176
if abspath(PROGRAM_FILE) == @__FILE__
166177
main()

0 commit comments

Comments
 (0)