On Tuesday, May 1, 2018 at 10:15:08 PM UTC+2, Ian Lance Taylor wrote:
>
> On Tue, May 1, 2018 at 1:00 PM, <[email protected] <javascript:>> wrote:
> >
> > I looked at the source but can't understand it well enough.
> >
> > If I allocate a [1000]byte, or a []byte, I understand the GC will need
> to
> > mark the slice/array itself for collection. However, does the GC mark
> all
> > the bytes within, and does the marking phase attempt to follow all the
> > items?
>
> A [1000]byte value holds no pointers, so the GC marks that value once
> and does not look inside it.
>
> A []byte holds a single pointer, to the backing array, and the backing
> array holds no pointers, so the GC marks the []byte and the backing
> array, and then stops.
>
>
>
Interesting. And what happens in this case:
type Foo struct {
s string
}
func makeFoo() *Foo {
buf := make([]byte,16)
foo := (*Foo)(unsafe.Pointer(&buf))
foo.s = "Hello World!"
return foo
}
If I understand you correctly, the GC would consider the string "Hello
World!" to be collectable, because its only pointer is in memory that
should not hold pointers. Or does the assignment of foo.s add some mark to
the memory block that it now may contain pointers?
If I changed buf to
buf := make([]unsafe.Pointer,16)
would that make things work? Now the GC knows that buf may contain pointers
and if I understand correctly it doesn't need to know the type of pointer.
MSB
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.