Go 1.24 runtime.Cleanup and sync.Pool.

196 views
Skip to first unread message

John

unread,
Jan 21, 2025, 9:10:16 PMJan 21
to golang-nuts
Curious if the new runtime.Cleanup() would allow auto sync.Pool.Put() for a variable.  I'm not sure from the documentation if this would work or would cause some type of issue (the variable was on its way to collection and then gets put in a pool.  I assume it would stop collection?).  The guarantees for running it seem to be better than Finalizer for this kind of thing (its working for file closures, so I'm guessing this will eventually happen), but still not sure how much better.

Thanks.




Ian Lance Taylor

unread,
Jan 21, 2025, 11:19:53 PMJan 21
to John, golang-nuts
On Tue, Jan 21, 2025 at 6:10 PM John <johns...@gmail.com> wrote:
>
> Curious if the new runtime.Cleanup() would allow auto sync.Pool.Put() for a variable. I'm not sure from the documentation if this would work or would cause some type of issue (the variable was on its way to collection and then gets put in a pool. I assume it would stop collection?). The guarantees for running it seem to be better than Finalizer for this kind of thing (its working for file closures, so I'm guessing this will eventually happen), but still not sure how much better.

It should be fine to use runtime.AddCleanup to add something to a
sync.Pool. That said, remember that runtime.AddCleanup is not called
with the pointer that is being freed. It is called on a different
pointer, that is presumably associated in some way.

Ian

John

unread,
Jan 22, 2025, 11:57:09 AMJan 22
to golang-nuts
Roger.  And thank you Ian.
Reply all
Reply to author
Forward
0 new messages