[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-08 Thread Jan
That makes sense that if you have only one finalizer in the cycle, the ordering could be respected, thanks for pointing it out! Now I still don't understand your explanation. The fact that the cycles can be arbitrarily deep is the same for normal GC, and it works without an issue. But I don't

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-07 Thread 'Michael Knyszek' via golang-nuts
On Tuesday, November 7, 2023 at 2:32:28 AM UTC-5 Jan wrote: Btw, I don't follow the sentence: "the GC necessarily has to keep referents of the object-to-be-finalized live even if the object isn't referenced anymore" That is true for objects not in cycles that need to be finalized as well.

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-07 Thread Harish Ganesan
I can completely understand the logic behind not freeing the objects with unclear ordering. But I expected that there will be some kind of upper limit / workaround for these kind of objects. But this means that using SetFinalizer on a big object, carelessly, can potentially lead to memory

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread Jan
Btw, I don't follow the sentence: "the GC necessarily has to keep referents of the object-to-be-finalized live even if the object isn't referenced anymore" That is true for objects not in cycles that need to be finalized as well. I'm not sure I follow the reasoning here ... Asking about it in

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread 'Michael Knyszek' via golang-nuts
Yes, cycles containing a finalizer aren't guaranteed to be freed. As others have pointed out, this is documented. SetFinalizer is really designed for a narrow set of use-cases, so concessions were made for overall GC performance. This case is one of them. IIUC, the core problem is a

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread Harish Ganesan
Does this behaviour mean that, those memory will never be freed and keep piling on ? That can be disastrous. On Monday, November 6, 2023 at 3:39:50 PM UTC+5:30 Jan wrote: > For what it's worth, a bit of "memory management" on structures in many > cases is very ok (not sure if in your case). So

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread Jan
For what it's worth, a bit of "memory management" on structures in many cases is very ok (not sure if in your case). So for your cyclic structure with finalizers, requiring the user of your code to call some "Finalize()" method (some destructor method you define) that manually breaks the cycle,

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread Jan
I was very surprised by this behavior of SetFinalizer: and indeed if you remove the SetFinalizer one can see that s1 is freed, because the memory reported by `m.HeapAlloc` goes back down. I think you already have the answer: the block that has the cycle (s1 and s2) have a SetFinalizer set, and

[go-nuts] Re: Why golang's garbage collector didn't free obj when an finalizer is associate with it?

2023-11-06 Thread j2gg0s
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 -