AFAIK it is allowed to re-slice past the len but it's rather not the way 
slices should be ever used. The word slice implies taking a portion from a 
bigger thing and that's always safe. When you append to the slice however, 
there's no new allocation if the underlying buffer still has place past the 
current slice len.

You missed the point about slices, I guess. Many slices can refer to the 
same internal buffer, giving you sort of window view of the data, thus as 
long as any slice still refer to the internal buffer GC won't collect it.


On Thursday, March 26, 2020 at 6:39:50 PM UTC+1, robfig wrote:
>
> Reducing a slice's length makes the elements unreachable, but the GC 
> appears to still treat them as live, based on this snippet:
> https://play.golang.org/p/SvsE-nXi-JA
>
> I would have expected the HeapInUse to go down in the second measurement. 
>
> Why is that? 
>
> I presume that the GC is traversing the slice's capacity rather than 
> length, but I'm not sure why this is necessary. Is there a way (without 
> using unsafe) to access slice elements past the length? 
>

-- 
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/27be27b0-5b55-4035-9953-072809fc7932%40googlegroups.com.

Reply via email to