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
- Callback and Promise Support in your Node.js Modules
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- How to Run an Asynchronous Function in Array.map()
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Filter Data in Streams
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- How to Reset and Empty an Array
- Human-Readable JSON.stringify() With Spaces and Line Breaks
- String Replace All Appearances
- Write a JSON Object to a File
- How to Create an Empty File
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- for…of vs. for…in Loops
- Get an Array With Unique Values (Delete Duplicates) (Coming soon)
- Run Async Functions in Batches (Coming soon)
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!