Skip to content

Memory usage spikes with source maps (--enable-source-maps) from v18.10.0 onwards #46140

@joelmukuthu

Description

@joelmukuthu

Version

18.10.0

Platform

5.4.0-135-generic #152-Ubuntu SMP Wed Nov 23 20:19:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

UPDATE: The steps below attempt to create a minimal reproduction of the issue, rather unsuccessfully, since the issue seems to be caused by large source maps and not merely throwing/handing an error. See #46140 (comment) for more info.

Obsolete minimal reproduction

Copy the following code blocks and save them as main.js and main.js.map and then run node --enable-source-maps main.js:

main.js:

// main.ts
var run = (withError) => {
  console.log(
    "Memory usage before (MB):",
    process.memoryUsage().heapUsed / 1024 / 1024
  );
  if (withError) {
    try {
      throw new Error("foo");
    } catch (e) {
      e.stack;
    }
  }
  console.log(
    "Memory usage after (MB):",
    process.memoryUsage().heapUsed / 1024 / 1024
  );
};
console.log("\nWithout error:");
run(false);
console.log("\nWith error:");
run(true);
console.log("\nWithout error:");
run(false);
console.log("\nWith error:");
run(true);
//# sourceMappingURL=main.js.map

main.js.map:

{
  "version": 3,
  "sources": ["main.ts"],
  "sourcesContent": ["const run = (withError) => {\n  console.log(\n    \"Memory usage before (MB):\",\n    process.memoryUsage().heapUsed / 1024 / 1024\n  );\n\n  if (withError) {\n    try {\n      throw new Error(\"foo\");\n    } catch (e) {\n      e.stack;\n    }\n  }\n\n  console.log(\n    \"Memory usage after (MB):\",\n    process.memoryUsage().heapUsed / 1024 / 1024\n  );\n};\n\nconsole.log(\"\\nWithout error:\");\nrun(false);\n\nconsole.log(\"\\nWith error:\");\nrun(true);\n\nconsole.log(\"\\nWithout error:\");\nrun(false);\n\nconsole.log(\"\\nWith error:\");\nrun(true);\n"],
  "mappings": ";AAAA,IAAM,MAAM,CAAC,cAAc;AACzB,UAAQ;AAAA,IACN;AAAA,IACA,QAAQ,YAAY,EAAE,WAAW,OAAO;AAAA,EAC1C;AAEA,MAAI,WAAW;AACb,QAAI;AACF,YAAM,IAAI,MAAM,KAAK;AAAA,IACvB,SAAS,GAAP;AACA,QAAE;AAAA,IACJ;AAAA,EACF;AAEA,UAAQ;AAAA,IACN;AAAA,IACA,QAAQ,YAAY,EAAE,WAAW,OAAO;AAAA,EAC1C;AACF;AAEA,QAAQ,IAAI,kBAAkB;AAC9B,IAAI,KAAK;AAET,QAAQ,IAAI,eAAe;AAC3B,IAAI,IAAI;AAER,QAAQ,IAAI,kBAAkB;AAC9B,IAAI,KAAK;AAET,QAAQ,IAAI,eAAe;AAC3B,IAAI,IAAI;",
  "names": []
}

On my machine, NodeJS v18.11.0 produces:

Without error:
Memory usage before (MB): 5.0794219970703125
Memory usage after (MB): 5.0850372314453125

With error:
Memory usage before (MB): 5.0877227783203125
Memory usage after (MB): 5.37274169921875

Without error:
Memory usage before (MB): 5.37542724609375
Memory usage after (MB): 5.377166748046875

With error:
Memory usage before (MB): 5.379974365234375
Memory usage after (MB): 5.411712646484375

While NodeJS v18.10.0 produces:

Without error:
Memory usage before (MB): 5.038932800292969
Memory usage after (MB): 5.044548034667969

With error:
Memory usage before (MB): 5.047233581542969
Memory usage after (MB): 5.0955810546875

Without error:
Memory usage before (MB): 5.0982666015625
Memory usage after (MB): 5.100006103515625

With error:
Memory usage before (MB): 5.102813720703125
Memory usage after (MB): 5.1345062255859375

When an error is thrown, the increase in memory usage on v18.11.0 spikes a bit higher than on v18.10.0. The same behaviour can be observed in later versions after v18.11.0.

For reference, without --enable-source-maps, this is the output on v18.11.0:

Without error:
Memory usage before (MB): 5.0529632568359375
Memory usage after (MB): 5.0585479736328125

With error:
Memory usage before (MB): 5.0612640380859375
Memory usage after (MB): 5.067131042480469

Without error:
Memory usage before (MB): 5.069816589355469
Memory usage after (MB): 5.071556091308594

With error:
Memory usage before (MB): 5.074363708496094
Memory usage after (MB): 5.080055236816406

And on v18.10.0:

Without error:
Memory usage before (MB): 5.012535095214844
Memory usage after (MB): 5.018119812011719

With error:
Memory usage before (MB): 5.020835876464844
Memory usage after (MB): 5.026702880859375

Without error:
Memory usage before (MB): 5.029388427734375
Memory usage after (MB): 5.0311279296875

With error:
Memory usage before (MB): 5.033935546875
Memory usage after (MB): 5.0396270751953125

How often does it reproduce? Is there a required condition?

It's consistent.

What is the expected behavior?

That memory usage doesn't spike when an error is handled by the application.

What do you see instead?

Memory usage spikes (ref: #46140 (comment)).

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    memoryIssues and PRs related to the memory management or memory footprint.source mapsIssues and PRs related to source map support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions