By the way, I don't think your snippet shows what you think it's showing. In particular, the output stays the same, even if you reduce the capacity to 0: https://play.golang.org/p/r2YvnDNsoBg I also always assumed the GC wouldn't do this optimization (throwing away memory if it's past the capacity of a slice) anyway. Not because it can't (I think), but just because I didn't think anyone built it to.
On Thu, Mar 26, 2020 at 6:47 PM Axel Wagner <axel.wagner...@googlemail.com> wrote: > > > On Thu, Mar 26, 2020 at 6:41 PM robfig <rob...@gmail.com> 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? >> > > Yes. https://play.golang.org/p/HhZWkGvR37L > > > >> -- >> 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/c2bea525-0636-4365-b551-59d1fceb5959%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/c2bea525-0636-4365-b551-59d1fceb5959%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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/CAEkBMfEWDfT8syfWM_n-9%2Be9tcs61xUgQoJ8EJzisUZCJEqLNA%40mail.gmail.com.