1

What is the maximum assignable length of an array in node.js? Why do these errors occur?

"FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory"

This error occurs when I execute this code.

const answer = [];

for (let i = 0; i < 20000; i++) {
  for (let j = 0; j < 20000; j++) {
    answer.push(1);
  }
}

I think it means that the length that can be assigned to the array is exceeded. Then, what is the maximum allocation length of an array in node.js?

Thanks!!! :)

1
  • 1
    check this out Commented Mar 24, 2021 at 11:32

1 Answer 1

2

There is a strict limitation for memory usage in the V8 JavaScript engine. the default are limits of ~512mb for 32-bit and ~1gb on 64-bit.

you can modify it like this for example:

node --max-old-space-size=4096 yourNodeFile.js

This will change the limitation to something like 4GB

Sign up to request clarification or add additional context in comments.

1 Comment

WOW, Thank you so much !! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.