JavaScript Development Space

How to solve: 'FATAL ERROR: JavaScript heap out of memory'

The error "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory" occurs when a Node.js process exceeds the default memory limit. This typically happens in memory-intensive tasks such as building large projects, handling extensive datasets, or running processes requiring significant resources.

Solution Options:

1. Increase the Memory Limit

By default, Node.js has a memory limit of approximately 1.5GB for 64-bit systems. You can increase this limit using the --max-old-space-size flag. For example, to allocate 4GB of memory:

bash
1 node --max-old-space-size=4096 your_script.js

For Windows:

Set NODE_OPTIONS="--max-old-space-size=8192"

For npm scripts, modify the command like this:

bash
1 node --max-old-space-size=4096 ./node_modules/.bin/your-command

If you're running a specific build tool like Webpack, try:

bash
1 node --max-old-space-size=4096 node_modules/.bin/webpack

The --max-old-space-size flag in Node.js can be set to control the maximum heap memory size. Here's how to use it:

bash
1 # Common values (in megabytes)
2 export NODE_OPTIONS="--max-old-space-size=2048" # 2 GB
3 export NODE_OPTIONS="--max-old-space-size=4096" # 4 GB
4 export NODE_OPTIONS="--max-old-space-size=8192" # 8 GB
5 export NODE_OPTIONS="--max-old-space-size=16384" # 16 GB
6
7 # You can use any value in megabytes, not just powers of 2:
8 export NODE_OPTIONS="--max-old-space-size=3000" # 3 GB
9 export NODE_OPTIONS="--max-old-space-size=5500" # 5.5 GB
10
11 # Check current heap size limit (in MB):
12 node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'

The value represents megabytes of memory. While powers of 2 (2048, 4096, etc.) are common, you can set any value up to your available system memory.

2. Update Dependencies

Old or inefficient libraries can cause memory issues. Update your project dependencies to their latest versions by running:

bash
1 npm outdated
2 npm update

3. Optimize the Code

  • Lazy Loading: Import only what you need at runtime.
  • Garbage Collection: Use smaller data structures or clear unused references.
  • Streams: If you're processing large files, use Node.js streams to avoid loading everything into memory.

4. Split Workloads

Break your tasks into smaller chunks or batches to reduce memory usage. For example:

  • Process large files line-by-line.
  • Use worker threads to parallelize resource-intensive tasks.

5. Use Monitoring Tools

Analyze the memory usage of your application:

  • process.memoryUsage(): Add this to your code to log memory stats:
    bash
    1 console.log(process.memoryUsage());
  • ools like clinic.js, Heapdump, or Chrome DevTools can help pinpoint memory leaks.

6. Clear Node Cache

If you’re building or running tasks repeatedly, clear the npm cache to free memory:

bash
1 npm cache clean --force

7. Upgrade Node.js

Newer Node.js versions have better memory management. Upgrade to the latest stable version:

bash
1 nvm install stable
2 nvm use stable

8. Use Swap Memory (Temporary)

If increasing memory limits doesn’t help and you’re on a server or limited device:

  • Add swap memory to your system as a fallback for RAM exhaustion. This is a temporary solution and not recommended for production environments.

Example: Increasing Memory for npm Run Build

If you're encountering this issue during a build process (e.g., npm run build), prepend the memory allocation flag:

bash
1 node --max-old-space-size=4096 ./node_modules/.bin/react-scripts build

Or modify the scripts section of package.json:

json
1 "scripts": {
2 "build": "node --max-old-space-size=4096 react-scripts build"
3 }
JavaScript Development Space

© 2025 JavaScript Development Space - Master JS and NodeJS. All rights reserved.