On Sat, Oct 15, 2016 at 12:08 AM,  <d...@veryhaha.com> wrote:
> On Saturday, October 15, 2016 at 8:18:04 AM UTC+8, Ian Lance Taylor wrote:
>> On Fri, Oct 14, 2016 at 4:08 PM, 'Peter Lam' via golang-nuts
>> <golan...@googlegroups.com> wrote:
>> > Is there someway to wait for all pending finalizers to be run?
>> Not in general, no.  Conceptually it doesn't make sense since, as you
>> know, finalizers not guaranteed to run at all.  You could of course
>> write your finalizers to support this.
> if finalizers not guaranteed to run at all, then what is its meaningfulness?

The current gc implementation has a concurrent GC.  A finalizer can
only run when the GC determines that the object is unreachable.  That
means that the question of when a finalizer runs is very nebulous.  If
the GC never completes--if it never determines that the objects is
unreachable--the finalizer will never run.  The GC only kicks in when
your program allocates enough new memory.  If your program never
allocates memory, the GC will never kick in, and the finalizer will
never run.  Many entirely reasonable Go programs do not allocate very
much memory.  Many of those programs use finalizers, because os.File
has a finalizer.  Those finalizers may never run.  They are still
meaningful, in that they will run if the program does allocate memory
and does complete a GC and does run finalizers.

To get back to your question, in the current gc implementation
finalizers are queued up to to be run by a goroutine.  That goroutine
simply runs each finalizer in order.  There is no way to wait for the
goroutine to complete.  In fact, it never completes; if there are no
finalizers to run, it goes to sleep waiting for the GC to release
another object with a finalizer.  There is also no way of telling
whether it has gone to sleep.


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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to