On Friday, January 10, 2020 at 1:02:22 AM UTC-8, Jan Mercl wrote: > > On Fri, Jan 10, 2020 at 9:52 AM Christophe Meessen > <christop...@gmail.com <javascript:>> wrote: > > > > It is possible to reduce the capacity of a slice by using the full slice > expression (https://golang.org/ref/spec#Slice_expressions). > > > > Now consider the following code where a is a 1MB slice. I then create b, > a slice of a, but with a much smaller capacity. Finally, I change the value > of a so that only b refers to the slice element storage. > > > > a := make([]byte, 1000000) > > b := b[:3:3] > > a = nil > > > > Will the garbage collector be able to recover the inaccessible space in > the slice element storage ? > > Specification does not directly define this, so the answer is - > possibly. AFAICT, no current Go compiler can free partial heap > objects.
Correct. > But there's one guarantee - the "dead" slice portion after > cap will not be scanned by the collector if no other live object uses > it. > That's not correct. If there is a reference to an object, the entire object is live and is scanned, regardless of what the reference(s) looks like (ptr, slice with small cap, slice with large cap). Again, this isn't in the spec - in principle the GC could not scan past the largest capacity. But we don't currently do that. -- 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/45788385-a6f0-4aa3-9889-31aef654ff71%40googlegroups.com.