GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: release per-child Compilation heap pressure in MultiCompiler · webpack/webpack@60a72b7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .changeset
    • multicompiler-release-per-child-memory.md
  • lib
    • Compiler.js
    • MultiCompiler.js
  • test
    • MultiCompiler.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.changeset/multicompiler-release-per-child-memory.md‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Release per-child `codeGenerationResults` in `MultiCompiler` and at `Compiler.close` to reduce memory retention.
Collapse file

‎lib/Compiler.js‎

Copy file name to clipboardExpand all lines: lib/Compiler.js
+26-3Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,25 @@ class Compiler {
491491
}
492492
}
493493

494+
/**
495+
* Release fields on a finished compilation that nothing reads after emit,
496+
* so the heap can shrink while user code still holds the Stats reference.
497+
* Recurses into child compilations. Stats output is preserved — only
498+
* codeGen byproducts are dropped.
499+
* @param {Compilation} compilation finished compilation to slim down
500+
* @returns {void}
501+
*/
502+
_releaseUnusedCompilationData(compilation) {
503+
for (const child of compilation.children) {
504+
this._releaseUnusedCompilationData(child);
505+
}
506+
// Rendered source per (module × runtime) — used only during seal/emit,
507+
// never read by Stats, and not serialized to the persistent cache.
508+
if (compilation.codeGenerationResults !== undefined) {
509+
compilation.codeGenerationResults.map.clear();
510+
}
511+
}
512+
494513
/**
495514
* Returns a compiler watcher.
496515
* @param {WatchOptions} watchOptions the watcher's options
@@ -1439,9 +1458,13 @@ ${other}`);
14391458
}
14401459
this.hooks.shutdown.callAsync((err) => {
14411460
if (err) return callback(err);
1442-
// Get rid of reference to last compilation to avoid leaking memory
1443-
// We can't run this._cleanupLastCompilation() as the Stats to this compilation
1444-
// might be still in use. We try to get rid of the reference to the cache instead.
1461+
// We can't run this._cleanupLastCompilation() as the Stats to this
1462+
// compilation might be still in use. We try to get rid of the reference
1463+
// to the cache instead, plus drop the seal/emit-phase codeGenerationResults
1464+
// map (Stats never reads it, the persistent cache never serializes it).
1465+
if (this._lastCompilation !== undefined) {
1466+
this._releaseUnusedCompilationData(this._lastCompilation);
1467+
}
14451468
this._lastCompilation = undefined;
14461469
this._lastNormalModuleFactory = undefined;
14471470
this.cache.shutdown(callback);
Collapse file

‎lib/MultiCompiler.js‎

Copy file name to clipboardExpand all lines: lib/MultiCompiler.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ module.exports = class MultiCompiler {
128128
doneCompilers--;
129129
}
130130
});
131+
// Release fields on this child's Compilation after every other
132+
// afterDone tap has had a chance to read them. stage: Infinity
133+
// makes us run last in the tapable iteration, which preserves
134+
// hook semantics for plugins that observe compilation state in
135+
// afterDone. Stats remains usable; only fields Stats never reads
136+
// (and that the persistent cache never serializes) are dropped.
137+
138+
compiler.hooks.afterDone.tap(
139+
{ name: CLASS_NAME, stage: Infinity },
140+
(stats) => {
141+
if (stats !== undefined) {
142+
compiler._releaseUnusedCompilationData(stats.compilation);
143+
}
144+
}
145+
);
131146
}
132147
this._validateCompilersOptions();
133148
}
Collapse file

‎test/MultiCompiler.test.js‎

Copy file name to clipboardExpand all lines: test/MultiCompiler.test.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ describe("MultiCompiler", () => {
141141
});
142142
});
143143

144+
it("should release per-child compilation memory as each child finishes (#15521)", (done) => {
145+
const compiler = createMultiCompiler();
146+
compiler.run((err, stats) => {
147+
if (err) return done(err);
148+
for (const childStats of stats.stats) {
149+
const compilation = childStats.compilation;
150+
// codeGenerationResults: only used during seal/emit, dropped.
151+
expect(compilation.codeGenerationResults.map.size).toBe(0);
152+
// Stats must still be usable on the slimmed compilation.
153+
expect(typeof childStats.toJson().hash).toBe("string");
154+
}
155+
compiler.close(done);
156+
});
157+
});
158+
144159
it("should watch again correctly after first compilation", (done) => {
145160
const compiler = createMultiCompiler();
146161
compiler.run((err, _stats) => {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.