From d9472e549a91c242c0c4639c2a15d2abaa6462f9 Mon Sep 17 00:00:00 2001 From: Tobias Grieger Date: Tue, 5 Oct 2021 13:05:41 +0000 Subject: [PATCH] kvserver: zero out slice before reuse Standard Go error - we were trying to avoid allocations by recycling a slice but weren't zeroing it out before. The occasional long slice that would reference a ton of memory would then effectively keep that large amount of memory alive forever. Touches #71050. Release note: None --- pkg/kv/kvserver/raft_transport.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/kv/kvserver/raft_transport.go b/pkg/kv/kvserver/raft_transport.go index 7eb0bb452be6..6798d2b789bf 100644 --- a/pkg/kv/kvserver/raft_transport.go +++ b/pkg/kv/kvserver/raft_transport.go @@ -503,12 +503,18 @@ func (t *RaftTransport) processQueue( } err := stream.Send(batch) - batch.Requests = batch.Requests[:0] - - atomic.AddInt64(&stats.clientSent, 1) if err != nil { return err } + + // Reuse the Requests slice, but zero out the contents to avoid delaying + // GC of memory referenced from within. + for i := range batch.Requests { + batch.Requests[i] = RaftMessageRequest{} + } + batch.Requests = batch.Requests[:0] + + atomic.AddInt64(&stats.clientSent, 1) } } }