SoFunction
Updated on 2025-03-09

A brief talk about the parameters max-old-space-size

Preface

Old space is the largest and most configurable part of the V8 managed (also known as garbage collection) heap (i.e. where JavaScript objects are located), and--max-old-space-sizeFlag controls its maximum size. As memory consumption approaches its limit, V8 will spend more time on garbage collection to free unused memory.

If the heap memory consumption (i.e., active objects that GC cannot free) exceeds the limit, V8 will crash your process (because there is a lack of an alternative), so you don't want to set it too low. Of course, if you set it too high, the extra heap usage that V8 will allow may cause your entire system to be out of memory (and swap or terminate random processes due to lack of alternatives).

Anyway, on machines with 2GB of memory, I might set --max-old-space-size to about 1.5GB to leave some memory for other purposes and avoid swap.

When I run "npm start" in the application, I get the following error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Most of the solutions released online are about increasing memory using NODE_OPTIONS="–max-old-space-size=2048" .

Set environment variables

NODE_OPTIONS --max-old-space-size The environment variable allows increasing the maximum heap size of the node. Setting the environment variable allows Node to read this value from your environment, so we don't need to pass this value as a parameter every time we run the Node command. This is set to a global value and can be used by each Node process.

Use the following command line to set environment variables:

export NODE_OPTIONS="–max-old-space-size=8192"

If you put this command in a terminal session, you need to do this in each new session. To avoid this, you can put it in a shell script file and the terminal will automatically load it for you.

.bashrcThe file exists in a Linux environment, and most comments will reload bash as a quick way, such as source ~/.bashrc , which loads environment variables in the current session. You can restart the terminal at any time to reload, but the former is preferred! Again, if using Windows, ignore this.

Command line method

Command line:

node --max_old_space_size=5000

The third method is based on the project

"scripts": 
{
    "start": "cross-env NODE_OPTIONS=--max-old-space-size=8192 webpack"
}

Summarize

This is the end of this article about the parameter max-old-space-size. For more related parameter max-old-space-size, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!