On Fri, Jan 10, 2020 at 9:52 AM Christophe Meessen
<christophe.mees...@gmail.com> 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. 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.

-- 
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/CAA40n-UqAuLnMZBOWbcswBUkgvFSNwOv58D9kpJGYdH0VVA7Qg%40mail.gmail.com.

Reply via email to