Repository navigation
Description
Bug report
NOTE: we observed this behavior on webpack 3.8/3.11, but the code appears identical for webpack@4, so I don't expect significant improvements when we can finally upgrade.
What is the current behavior?
Our largest webpack build was running into JS out of memory error while emitting assets that included sourcemaps (edit: when we turned off sourcemaps, we didn't see OOME, but we did see a massive spike in memory usage at the end of build as assets were being written)
By digging through the source of Compiler.js and CachedSource.js (from webpack-sources), I found that the (very large) bundles being emitted were being cached in memory by this call: https://github.com/webpack/webpack/blob/master/lib/Compiler.js#L331
I applied a very hacky, very small patch to Compiler.js to verify my assumptions that basically just force-clears CachedSource's internal state after calling .source() at Compiler.js:331 (since it's never used again, AFAICT):
--- node_modules/webpack/lib/Compiler.js.orig 2018-07-10 11:55:49.000000000 -0700
+++ node_modules/webpack/lib/Compiler.js 2018-07-10 11:56:05.000000000 -0700
@@ -329,6 +329,10 @@
return callback();
}
let content = source.source();
+ // reset cache for 'source', since we're never going to look at it again
+ source._cachedSource = undefined;
+ source._cachedSize = undefined;
+ source._cachedMaps = {};
if(!Buffer.isBuffer(content)) {
content = new Buffer(content, "utf8"); // eslint-disable-line
This brought our memory usage (as measured directly from the heap snapshot) from 5.3GB --> 1.7GB at a breakpoint immediately after webpack finished.
If someone who knows the code better than I do can verify my belief that not using the cache of CachedSources for emitting assets is safe, it might make sense to modify CachedSource to have a "don't build cache for this call" API, or something? Since they can always be reproduced if needed, it seems like it should be safe, though there could be cases I'm not thinking of where it's more expensive in CPU time.
In any event,
If the current behavior is a bug, please provide the steps to reproduce.
For us, repro steps were:
- run build that included cheap-module-eval-source-map
- build OOMEs most of the time while emitting assets
What is the expected behavior?
No OOME, ever. :)
Other relevant information:
webpack version: 3.8 and 3.11
Node.js version: node 8.9.x
Operating System: MacOS + Linux
Additional tools: we're using sourcemaps and extract-text-webpack-plugin w/ webpack 3.x, if that's relevant here. (Sourcemaps definitely exacerbate the issue; I haven't tested explicitly if ETWP matters, since the heap snapshot indicated the bundled JS existing in memory as the culprit)
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
montogeek commentedon Jul 11, 2018
What is OOME?
roblg commentedon Jul 11, 2018
Sorry for not specifying -- "Ouf of Memory Error"
It usually looks like:
plus a stack trace
webpack-bot commentedon Apr 10, 2019
This issue had no activity for at least half a year.
It's subject to automatic issue closing if there is no activity in the next 15 days.
alexander-akait commentedon Apr 10, 2019
Please try with latest webpack version, if you still have problem, please open new issue with reproducible test repo, thanks