The system process of Node.js starts your applications with a default memory limit. If you’re going to run an application that saves a lot of data into variables and therefore memory, you may run into a Node.js process exit due to allocation issues.
Increasing Node’s memory limit is the topic of this tutorial and you’ll find the problem description and solution following below.
Node.js Series Overview
- String Replace All Appearances
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Get an Array With Unique Values (Delete Duplicates)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
Running a memory dependent app consumes all available JS heap space during its processing. You’ll receive an error output and message like this:
Actually, there’s a quite simple fix! The next section guides you through the single step that’s required to make your app work.
The solution to run your Node.js app with increased memory is to start the process with an additional V8 flag:
--max-old-space-size. You need to append your desired memory size in megabytes.
The following command will start the
server.js with a memory limit of 8 GB.
node --max-old-space-size=8192 server.js
Please notice the order of arguments after the
node command. You need to add desired parameters first to adjust the related values appropriately. Then, start the file that should run as a Node.js process.
The solution to increase the memory limit is kind of straight forward. The hardest part of the problem is to remember the name of the flag. Honestly, that’s the reason I’m writing this tutorial, because I need to Google the flag every time I want to use it!
Nonetheless, hopefully you’ll benefit from this short guide and bookmark it for fast access ;-)
We appreciate feedback and love to help you if there’s a question in your mind. Let us know in the comments or on twitter @futurestud_io.
Make it rock & enjoy coding!