From 8870141923439c372520aa602d307c9a312c6dab Mon Sep 17 00:00:00 2001 From: Jack Phallen Date: Sun, 11 Jan 2026 10:26:57 -0500 Subject: [PATCH] Cap entryChan buffer to prevent memory spikes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cap buffer at 4096. All entries still process normally—producers just briefly block if the buffer fills, which is negligible since the collector drains it quickly. --- cmd/analyze/scanner.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/analyze/scanner.go b/cmd/analyze/scanner.go index 0d7ddca..af77d60 100644 --- a/cmd/analyze/scanner.go +++ b/cmd/analyze/scanner.go @@ -53,7 +53,15 @@ func scanPathConcurrent(root string, filesScanned, dirsScanned, bytesScanned *in var wg sync.WaitGroup // Collect results via channels. - entryChan := make(chan dirEntry, len(children)) + // Cap buffer size to prevent memory spikes with huge directories. + entryBufSize := len(children) + if entryBufSize > 4096 { + entryBufSize = 4096 + } + if entryBufSize < 1 { + entryBufSize = 1 + } + entryChan := make(chan dirEntry, entryBufSize) largeFileChan := make(chan fileEntry, maxLargeFiles*2) var collectorWg sync.WaitGroup