From d207c977d92d53b4b2cd2f8ef5068886f3a7cac6 Mon Sep 17 00:00:00 2001 From: LevTheSWag Date: Tue, 23 Sep 2025 15:46:08 +0700 Subject: [PATCH] fix: add memory management to prevent heap overflow during large site builds - Add periodic cache clearing every 50 pages during pMap processing - Reduce build concurrency to max 10 to manage memory usage - Add optional garbage collection calls when available - Include progress logging for memory cleanup operations Fixes #4833 --- src/node/build/build.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 364c7490..5fbf80ed 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -136,6 +136,11 @@ export async function build( const usedIcons = new Set() + // Memory management: track processed pages for periodic cleanup + let processedCount = 0 + const totalPages = ['404.md', ...siteConfig.pages].length + const cleanupInterval = 50 + await pMap( ['404.md', ...siteConfig.pages], async (page) => { @@ -152,8 +157,22 @@ export async function build( additionalHeadTags, usedIcons ) + + // Memory management: periodic cache clearing and garbage collection + processedCount++ + if (processedCount % cleanupInterval === 0) { + siteConfig.logger.info( + `Processed ${processedCount}/${totalPages} pages, clearing cache to manage memory...` + ) + clearCache() + + // Optional garbage collection hint + if (global.gc) { + global.gc() + } + } }, - { concurrency: siteConfig.buildConcurrency } + { concurrency: Math.min(10, siteConfig.buildConcurrency || 10) } ) const icons = require('@iconify-json/simple-icons/icons.json')