Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does go garbage collect parts of slices?

Tags:

If I implement a queue like this...

package main  import(     "fmt" )  func PopFront(q *[]string) string {     r := (*q)[0]     *q = (*q)[1:len(*q)]     return r }  func PushBack(q *[]string, a string) {     *q = append(*q, a) }  func main() {     q := make([]string, 0)      PushBack(&q, "A")     fmt.Println(q)     PushBack(&q, "B")     fmt.Println(q)     PushBack(&q, "C")     fmt.Println(q)      PopFront(&q)     fmt.Println(q)     PopFront(&q)     fmt.Println(q)       } 

... I end up with an array ["A", "B", "C"] that has no slices pointing to the first two elements. Since the "start" pointer of a slice can never be decremented (AFAIK), those elements can never be accessed.

Is Go's garbage collector smart enough to free them?

like image 747
Timmmm Avatar asked Feb 10 '15 13:02

Timmmm


People also ask

How do you use slices in Go?

In Go, there are two functions that can be used to return the length and capacity of a slice: len() function - returns the length of the slice (the number of elements in the slice) cap() function - returns the capacity of the slice (the number of elements the slice can grow or shrink to)

Are Go routines garbage collected?

- Goroutines are not garbage collected. - In this case and many others, you can just use a buffered channel. But of course in many other cases it doesn't solve the problem. - Several goroutines can try to send to the same "quit" channel.

How does slice capacity work in Golang?

A slice can also be initialized with the built-in make() function that takes the type of a slice as the first argument and the length as the second. The resulting slice has a capacity equals to the length, and the underlying array is initialized with zero values.

How is garbage collection done in Go?

To do this, Go relies on a garbage collector. Garbage collectors have two key parts, a mutator and a collector. The collector executes garbage collection logic and finds objects that should have their memory freed. The mutator executes application code and allocates new objects to the heap.


1 Answers

Slices are just descriptors (small struct-like data structures) which if not referenced will be garbage collected properly.

The underlying array for a slice (to which the descriptor points to) on the other hand is shared between all slices that are created by reslicing it: quoting from the Go Language Specification: Slice Types:

A slice, once initialized, is always associated with an underlying array that holds its elements. A slice therefore shares storage with its array and with other slices of the same array; by contrast, distinct arrays always represent distinct storage.

Therefore if at least one slice exists, or a variable holding the array (if a slice was created by slicing the array), it will not be garbage collected.

Official Statement about this:

The blog post Go Slices: usage and internals By Andrew Gerrand clearly states this behaviour:

As mentioned earlier, re-slicing a slice doesn't make a copy of the underlying array. The full array will be kept in memory until it is no longer referenced. Occasionally this can cause the program to hold all the data in memory when only a small piece of it is needed.

...

Since the slice references the original array, as long as the slice is kept around the garbage collector can't release the array.

Back to your example

While the underlying array will not be freed, note that if you add new elements to the queue, the built-in append function occasionally might allocate a new array and copy the current elements to the new – but copying will only copy the elements of the slice and not the whole underlying array! When such a reallocation and copying occurs, the "old" array may be garbage collected if no other reference exists to it.

Also another very important thing is that if an element is popped from the front, the slice will be resliced and not contain a reference to the popped element, but since the underlying array still contains that value, the value will also remain in memory (not just the array). It is recommended that whenever an element is popped or removed from your queue (slice/array), always zero it (its respective element in the slice) so the value will not remain in memory needlessly. This becomes even more critical if your slice contains pointers to big data structures.

func PopFront(q *[]string) string {     r := (*q)[0]     (*q)[0] = ""  // Always zero the removed element!     *q = (*q)[1:len(*q)]     return r } 

This is mentioned Slice Tricks wiki page:

Delete without preserving order

a[i] = a[len(a)-1]  a = a[:len(a)-1] 

NOTE If the type of the element is a pointer or a struct with pointer fields, which need to be garbage collected, the above implementations of Cut and Delete have a potential memory leak problem: some elements with values are still referenced by slice a and thus can not be collected.

like image 145
icza Avatar answered Sep 19 '22 18:09

icza