In a previously published PM2 tutorial, you’ve learned how to start a Node.js process with arguments. In that tutorial, we’ve used the idea to reduce the memory limit of Node.js to let the garbage collector clean up earlier.
Well, sometimes you don’t see the forest inbetween trees and that the used example would be a good tutorial by itself. That’s the reason we’ve decided to extract the example to increase the memory limit while starting a Node.js process using PM2’s command line interface.
PM2 Series Overview
- How to Start Your App With Node.js V8 Arguments
- Use NPM to Start Your App
- Start Node.js App With Increased Memory Limit From Command Line
- Start Node.js App With Increased Memory Limit From JSON Configuration
- Start Multiple Apps With A Single Process File (JSON/JS/YAML)
Start Your App With Increased Memory Limit
In situations where you just want to execute a short-lived Node.js app, you might use the
node command directly and increase the memory limit during the app start.
Of course you can increase the memory limit for your Node.js app using PM2’s functionality to manage your processes. Use the
--node-args flag while starting your app from the command line. That flag expects a list of values with a space as separator. The following code snippet illustrates how to start your Node.js app with increased memory limit to 8 GB.
pm2 start my_app.js --node-args="--max_old_space_size=8192"
You need to replace
my_app.js with your file that kicks off your Node.js application. Also, you might want to adjust the memory limit to a value that suites your expectations or system resources.
And that’s all the magic :)
Increasing the memory limit is useful in various scenarios. Combining the process management capabilities of PM2 and adjusting the default values of the Node.js system process quite straight forward. PM2 got your back to pass desired Node.js arguments during the process start.
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!