Just my(newbie of GC) guess:

In the mark stage,
- GC find s1's finalizer and invoke scanobject to scan s1
  https://github.com/golang/go/blob/go1.21.1/src/runtime/mgcmark.go#L391
  https://github.com/golang/go/blob/go1.21.1/src/runtime/mfinal.go#L484
- then find s2 and queue it to be scanned
- then find s1

Avoid SetFinalizer reference s1 
https://go.dev/play/p/37cihOahxFk


在2023年11月5日星期日 UTC+8 23:01:14<Soren Yang> 写道:

> As shown in the following code:
>
> cyclic structure with finalizer <https://go.dev/play/p/Fn_h08y-L6b>
>
> The s1 & s2 didn't been free, and finalizer didn't run. But when enable 
> the line which have been commented, all run as expected(s1 & s2 been free)。
>
> I have seen the comment in runtime.SetFinalizer: If a cyclic structure 
> includes a block with a finalizer, that cycle is not guaranteed to be 
> garbage collected and the finalizer is not guaranteed to run, because there 
> is no ordering that respects the dependencies.
>
> But why it haven't been free in first code?
>

-- 
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/a323cb66-5cc1-45c2-b65f-35cad3a84965n%40googlegroups.com.

Reply via email to