本文介绍了清除golang中的切片可保证垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现基于时间的槽来保存使用golang切片的数据。我设法想出了这样一个去程序,它也有效。但是我对垃圾收集和这个程序的一般性能几乎没有问题。这个程序是否保证一旦切片等于零就垃圾收集物品?在洗牌时,我希望这个程序不会进行任何深层复制。

  type DataSlots struct {
slotDuration int //以毫秒为单位
slots [] [] interface {}
totalDuration int //以毫秒为单位
}

func新(slotDur int,totalDur int)* DataSlots {
dat:=& DataSlots {slotDuration:slotDur,
totalDuration:totalDur}
n:= totalDur / slotDur
dat.slots = make([] [] interface {},n)
for i:= 0;我< N; i ++ {
dat.slots [i] = make([] interface {},0)
}
go dat.manageSlots()
return dat
}

func(self * DataSlots)addData(data interface {}){
self.slots [0] = append(self.slots [0],data)
}

//这应该是一个去例程
func(self * DataSlots)manageSlots(){
n:= self.totalDuration / self.slotDuration $ b $ for {
time.Sleep(time.Duration(self.slotDuration)* time.Millisecond)
for i:= n - 1;我> 0; i-- {
self.slots [i] = self.slots [i-1]
}
self.slots [0] =零
}
}

我删除了此片段中的关键部分处理,以使其更为简洁。


一旦你的slice设置为 nil ,片中包含的任何值都可用于垃圾回收,前提是底层数组不与另一个slice共享。

由于程序中没有切片操作,因此您永远不会有多个对同一个数组的引用,也不会您将数据留在底层数组的任何不可访问的部分。



您需要注意的是,当您使用切片操作时:

  a:= [] int {1,2,3,4} 
b:= a [1:3]
a = nil
//值1和4无法收集,因为它们是
//仍然包含在b的底层数组中
$ bc:= [] int {1,2,3,4}
c = append( c [1:2],5)
// c现在是[] int {2,5},但是值1和4仍然是
// //仍在底层数组中。 4可能被后面的追加覆盖
//,但是1不可访问且不会被
//收集,直到复制底层数组。

append 的片段不足时,只有片段中包含的值被复制。没有任何值的深层副本。


I wanted to implement time based slots for holding data using golang slices. I managed to come up with a go program like this and it also works. But I have few questions regarding garbage collection and the general performance of this program. Does this program guarantee garbage collection of items once slice is equated to nil? And while shuffling slices, I hope this program does not do any deep copying.

type DataSlots struct {
    slotDuration  int //in milliseconds
    slots         [][]interface{}
    totalDuration int //in milliseconds
}

func New(slotDur int, totalDur int) *DataSlots {
    dat := &DataSlots{slotDuration: slotDur,
        totalDuration: totalDur}
    n := totalDur / slotDur
    dat.slots = make([][]interface{}, n)
    for i := 0; i < n; i++ {
        dat.slots[i] = make([]interface{}, 0)
    }
    go dat.manageSlots()
    return dat
}

func (self *DataSlots) addData(data interface{}) {
    self.slots[0] = append(self.slots[0], data)
}

// This should be a go routine
func (self *DataSlots) manageSlots() {
    n := self.totalDuration / self.slotDuration
    for {
        time.Sleep(time.Duration(self.slotDuration) * time.Millisecond)
        for i := n - 1; i > 0; i-- {
            self.slots[i] = self.slots[i-1]
        }
        self.slots[0] = nil
    }
}

I removed critical section handling in this snippet to make it concise.

解决方案

Once your slice is set too nil, any values contained in the slice are available for garbage collection, provided that the underlying array isn't shared with another slice.

Since there are no slice operations in your program, you never have multiple references to the same array, nor are you leaving data in any inaccessible portions of the underlying array.

What you need to be careful of, is when you're using slice operations:

a := []int{1, 2, 3, 4}
b := a[1:3]
a = nil
// the values 1 and 4 can't be collected, because they are
// still contained in b's underlying array

c := []int{1, 2, 3, 4}
c = append(c[1:2], 5)
// c is now []int{2, 5}, but again the values 1 and 4 are
// still in the underlying array. The 4 may be overwritten
// by a later append, but the 1 in inaccessible and won't
// be collected until the underlying array is copied.

While append does copy values when the capacity of the slice in insufficient, only the values contained in the slice are copied. There is no deep copy of any of the values.

这篇关于清除golang中的切片可保证垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:44