On Sun, May 24, 2020 at 12:50 PM Justin Israel <justinisr...@gmail.com> wrote:
>
> About the frequency of finalizers, can you confirm my question about whether 
> the finalizers run during the GC run each time, regardless of when that GC 
> run decides to happen? Or can a GC start to run but decide not to process the 
> finalizers yet in that cycle?

The question doesn't have a simple answer.  The garbage collector runs
concurrently with the program.  The exact details aren't too important
(and I'm not sure I fully understand them) but the collector will
periodically identify blocks of memory that have been fully marked,
such that any unmarked object in that block is definitely free.  If
one of those unmarked objects has a finalizer, the collector will mark
the object so that it will not be freed after all, will remove the
finalizer from the object, and will queue the finalizer to be run.
The finalizer queue will hold a reference to the object, so it won't
be freed while the finalizer is queued.  There is a separate goroutine
that runs finalizers, and queuing a finalizer wakes up that goroutine.
When that goroutine is scheduled, it will get the finalizer off the
queue and run it.

I'm not sure how to take that process and turn it into an answer to
your question.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXu-vxk1cLFSzVi%2BjS3CTrNsqqRH46ypzZhFC5Ua75sgQ%40mail.gmail.com.

Reply via email to