FIX: fix memory leak

This commit is contained in:
kbearXD 2024-09-03 15:34:55 +08:00
parent 50262f2a84
commit 0e68cb1f7e

View File

@ -5,6 +5,8 @@ import "github.com/c9s/bbgo/pkg/types"
const MaxNumOfKLines = 5_000
const MaxNumOfKLinesTruncate = 100
const CapacityOfKLineWindowLimit = 5_000
// MarketDataStore receives and maintain the public market data of a single symbol
//go:generate callbackgen -type MarketDataStore
type MarketDataStore struct {
@ -57,8 +59,13 @@ func (store *MarketDataStore) AddKLine(k types.KLine) {
}
window.Add(k)
if len(*window) > MaxNumOfKLines {
*window = (*window)[MaxNumOfKLinesTruncate-1:]
lenOfWindow := len(*window)
capOfWindow := cap(*window)
if lenOfWindow == capOfWindow && capOfWindow > CapacityOfKLineWindowLimit {
truncatedWindow := (*window)[(CapacityOfKLineWindowLimit / 2):]
copy(*window, truncatedWindow)
*window = (*window)[:len(truncatedWindow)]
}
store.EmitKLineClosed(k)