"The primary use-cases for weak pointers <https://pkg.go.dev/weak#Pointer> 
are for implementing caches"

I'm trying to create objects and insert a weak pointer to each of them in a 
slice. The slice then contains the only reference to each object, thus it 
is possible for each pointer's value to become nil anytime, and the 
objects' memory garbage collected.

In practice, on my laptop (macOS, go 1.24.1) the weak pointers tend to 
become nil very fast, before I'm finished inserting 400 objects in the 
slice, which seems to be "as soon as the object becomes unreachable".  
While this observation is consistent with the documentation, I'm wondering 
how I can effectively build a cache when the data tends to go away as soon 
as it is inserted?

Here is my code <https://go.dev/play/p/mKxiOvI_IJh>, which behaves a bit 
differently in the playground but the general observation remains.

-- 
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 visit 
https://groups.google.com/d/msgid/golang-nuts/1a129699-ce4a-4deb-bb83-ea2fa484d83bn%40googlegroups.com.

Reply via email to