On Thu, Jul 18, 2019 at 7:24 AM robfig <rob...@gmail.com> wrote: > > I was curious if reducing the capacity of slice makes that memory available > to be reclaimed? > > (1) Reducing capacity using a three-index: > > var s = make([]int64, 1e9) > ... add data to s, keeping a count .. > s = s[:n:n] // reduce length and capacity to number of items read > > (2) Reducing capacity by slicing off the front, as happens when using slice > as a queue > > var s []int64 > s = append(s, 1, 2, 3, 4, 5) > s = s[5:] > > Should I be careful to allocate a new slice and copy over, if I want to avoid > that memory being pinned until the original slice goes out of scope?
In the current implementations, reducing the capacity of the slice does not cause the trailing memory to be released. Basically, it's hard to be sure that there isn't any other slice somewhere that might permit references to that trailing memory. There has been some discussion of changing the garbage collector to support this, but it's not simple. Ian -- 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/CAOyqgcUzK4g1RK9XmGLN%3DcSAEqdby2u1xta12-wECvnOFeL4tg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.