Skip to content

Commit

Permalink
FIX: fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
kbearXD committed Sep 4, 2024
1 parent 50262f2 commit 0e68cb1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/bbgo/marketdatastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0e68cb1

Please sign in to comment.