r/javascript • • Oct 11 '17

JavaScript's Promise is Memory Unsafe

https://alexn.org/blog/2017/10/11/javascript-promise-memory-unsafe.html
70 Upvotes

77 comments sorted by

View all comments

13

u/lachlanhunt Oct 11 '17

I tried that code in node 7.10.1 and 8.6.0. It didn't crash at all. It just endlessly printed numbers until I killed it.

11

u/alexelcu Oct 11 '17 edited Oct 11 '17

Proof:

$ nvm use v7.10.1

$ $ node --version
v7.10.1

$ node bench.js

...
2360000
2361000

<--- Last few GCs --->

[21305:0x104000000]    52690 ms: Mark-sweep 1408.5 (1467.0) -> 1408.5 (1436.0) MB, 1840.4 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1840 ms) last resort 
[21305:0x104000000]    54684 ms: Mark-sweep 1408.5 (1436.0) -> 1408.5 (1436.0) MB, 1993.3 / 0.0 ms  last resort 
...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Also see a screenshot of Activity Monitor showing the process taking 1.5 GB of RAM.

If you try that in Chrome, it will crash faster, as Node.js is less conservative being configured for the server-side, so what happens is that the process first grinds to a halt, with the GC desperately trying to deallocate memory.

Cheers,