perf(build): add memory management during page rendering to prevent OOM errors #4955
+20
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements memory management optimizations during the build process to prevent "JavaScript heap out of
memory" errors when building large documentation sites with extensive mathematical formulas.
Changes:
src/node/build/build.ts
global.gc
is availableRoot Cause: The LRU cache accumulates heavy MarkdownCompileResult objects (max: 1024 entries) and math
processing creates memory-intensive objects per page. Without intermediate memory cleanup during the build
process, large sites with math formulas exhaust available memory.
Solution: Periodic cache clearing and garbage collection hints during the pMap loop, combined with reduced
concurrency to prevent memory pressure.
Linked Issues
fixes #4833
Additional Context
This fix specifically targets builds with extensive mathematical formulas using markdown-it-mathjax3, which
creates particularly memory-intensive objects. The 50-page cleanup interval and concurrency limit of 10
provide a good balance between build performance and memory management.
Please test with large documentation sites containing math formulas to verify the memory usage improvements.
Tip
The author of this PR can publish a preview release by commenting
/publish
below.The key improvements from the original: