Skip to content

OOME while emitting assets #7703

Closed
Closed

Description

@roblg

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)

Activity

  1. montogeek commented on Jul 11, 2018

    @montogeek
    Contributor

    What is OOME?

  2. roblg commented on Jul 11, 2018

    @roblg
    Author

    What is OOME?

    Sorry for not specifying -- "Ouf of Memory Error"

    It usually looks like:

    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
    

    plus a stack trace

  3. webpack-bot commented on Apr 10, 2019

    @webpack-bot
    Contributor

    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.

  4. added
    status: stale
    on Apr 10, 2019
  5. alexander-akait commented on Apr 10, 2019

    @alexander-akait
    Member

    Please try with latest webpack version, if you still have problem, please open new issue with reproducible test repo, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: stale

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions