股票代码仍然存在于记忆中
ticker still live in memory
为什么当我初始化自动收报机然后更新它时,原始自动收报机仍然存在于内存中,如果我进行循环,它会很快耗尽我的 RAM
timeOut := 10
ticker := time.NewTicker(time.Duration(timeOut) * time.Second)
for {
ticker = time.NewTicker(time.Duration(timeOut) * time.Second)
}
import "time"
func NewTicker(d Duration) *Ticker
NewTicker returns a new Ticker containing a channel that will send the
time with a period specified by the duration argument. It adjusts the
intervals or drops ticks to make up for slow receivers. The duration d
must be greater than zero; if not, NewTicker will panic. Stop the
ticker to release associated resources.
按照说明进行操作:停止自动收报机以释放相关资源。
for {
ticker = time.NewTicker(time.Duration(timeOut) * time.Second)
}
这将尽可能快地创建代码实例,并且由于代码对象的性质,它们将无法清理(它们被 ticket 的实现引用,由于其异步性质,发生在不同的 goroutine 中)。 Go 可以非常快速地执行此操作,从而非常迅速地导致 OOM(内存不足)错误。
但是这个模式没有任何意义。没有充分的理由在 for
循环中创建这样的新代码。您可以创建多个代码,甚至很多,但您不能创建无限的代码,这样做也没有多大意义。
此外,请注意您实际上并没有在任何地方使用您的代码。考虑这里的例子: https://gobyexample.com/tickers 。您将看到它们如何与自动收报机交互,以便在每次报价时生成代码 运行。请注意,这里的自动收报机只创建了 一次,而不是在循环中。
为什么当我初始化自动收报机然后更新它时,原始自动收报机仍然存在于内存中,如果我进行循环,它会很快耗尽我的 RAM
timeOut := 10
ticker := time.NewTicker(time.Duration(timeOut) * time.Second)
for {
ticker = time.NewTicker(time.Duration(timeOut) * time.Second)
}
import "time"
func NewTicker(d Duration) *Ticker
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
按照说明进行操作:停止自动收报机以释放相关资源。
for {
ticker = time.NewTicker(time.Duration(timeOut) * time.Second)
}
这将尽可能快地创建代码实例,并且由于代码对象的性质,它们将无法清理(它们被 ticket 的实现引用,由于其异步性质,发生在不同的 goroutine 中)。 Go 可以非常快速地执行此操作,从而非常迅速地导致 OOM(内存不足)错误。
但是这个模式没有任何意义。没有充分的理由在 for
循环中创建这样的新代码。您可以创建多个代码,甚至很多,但您不能创建无限的代码,这样做也没有多大意义。
此外,请注意您实际上并没有在任何地方使用您的代码。考虑这里的例子: https://gobyexample.com/tickers 。您将看到它们如何与自动收报机交互,以便在每次报价时生成代码 运行。请注意,这里的自动收报机只创建了 一次,而不是在循环中。