PM2 — Start Node.js App With Increased Memory Limit From JSON Configuration

There are already two related PM2 tutorials available that show you how to start a Node.js process with V8 arguments and how to start your Node.js application with increased memory limit from the command line. Depending on whether you’re starting your applications using a JSON config file for PM2, you should read on and get to know how to increase the memory limit for your application with such a configuration.

PM2 Series Overview

Start Your App With Increased Memory Limit

If you just want to run a short-lived Node.js process, you might use the node command directly. In those situations, you’re able to just use V8 arguments to increase the Node.js process memory limit as described in the linked tutorial.

For long running processes, PM2 is a great fit. Especially, if you rely on a complex configuration that you just want to create once and save it in a JSON file. To increase Node’s memory limit using PM2, you need to add the node_args key to your app configuration and pass a string with valid V8 arguments including respective values. The following code block shows you a precise example:

{
  "apps": [
    {
      "name": "futurestudio-homepage",
      "script": "./homepage/server.js",
      "node_args": "--max_old_space_size=8192"
    }
  ]
}

Using the JSON config from above with PM2, the server.js file located within the homepage folder will be started by PM2 with the defined app name futurestudio-homepage. Further, the Node.js process has an increased memory limit of 8 GB.

Notice the dashes before max_old_space_size=8192 are required to represent actual V8 arguments.

Outlook

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 is quite straight forward. PM2 got your back to pass desired Node.js arguments during the process start within a JSON config file.

Have a question about PM2 or Node.js in general? Just let us know on Twitter @futurestud_io or leave a comment below.

Make it rock & enjoy coding!

Explore the Library

Find interesting tutorials and solutions for your problems.