NodeJS

    NodeJS heap out of memory


At times, a NodeJS application will also take up more memory than it's been allocated for, especially when working with large amounts of data or performing memory-intensive operations. This typically causes an error that appears in the form of:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory  

Why this error occurs

By default, NodeJS allocates a limited amount of memory for applications, and if your app exceeds this, it triggers the "heap out of memory" error. NodeJS is designed to handle most server-side applications efficiently, but when handling larger data, intensive computations, or memory leaks, it may require a larger heap size than the default allocation.

Solutions to the "NodeJS Heap Out of Memory" Error

NodeJS Heap Out of Memory Solutions

 

1. Increase Memory

The easiest way is to increase the NodeJS available memory. You can set up the memory limit at runtime while running your Node app. For example, you can increase the heap size to 4 G:

node --max-old-space-size=4096 app.js

Set the --max-old-space-size to your own value according to the need of memory in your application.

Tip: Do not allocate too much memory beyond what your system can actually hold, for the swapping may happen and performance is severely degraded.

 

2. Optimizing Your Code for Memory Usage

In case that the error occurs because of memory leaks or a poorly done code, that shall solve the issue of the repeat itself. You can get some recommendations on how to optimize your memory usage with the following:

Do not retain objects or variables if they are not required. Release the memory of arrays or object references that are no longer in use.

Use fewer memory-hungry libraries: Some libraries, such as imaging or data processing, require a lot of memory. Find a replacement or process them on bits and pieces.

Stream your files: Do not read huge files. Instead, let streams read them bit by bit and process them. 

3. Find Memory Leaks

The most common cause of memory usage is a memory leak. The next chapters explain how to track and then debug memory leaks:

Heap snapshots: Through the Chrome DevTools, or the node-inspect module, take heap snapshots so that the memory usage can be analyzed.

Profiling: Utilize NodeJS's built-in profiler or third-party Clinic.js, to determine which functions are slow or hogs plenty of memory.

 

4. Clustering in Load Balancing Running several instances of a NodeJS application in multiple processes can prevent individual processes from hitting memory limits. Node's cluster module is another way to fork processes to spread your workload across multiple CPU cores. You could do something like this: 

const cluster = require('cluster');const os = require('os');if (cluster.isMaster) {  const numCPUs = os.cpus().length;  for (let i = 0; i < numCPUs; i++) {    cluster.fork();  }} else {  // App logic here}

5. Use the v8 Garbage Collection Flags NodeJS uses V8's garbage collection, which is normally good but sometimes requires adjustment for high-memory applications:

--expose-gc: It executes the garbage collector by the global.gc() method. This should be done with caution because it can influence performance.

--max-semi-space-size: This sets the size of the V8 garbage collection space. Sometimes, this might be needed for fine-tuning performance.

Conclusion

The "heap out of memory" error is indeed frustrating, but oftentimes it just indicates that your application needs optimization or just more memory resources. Proper understanding of memory usage patterns and incremental sequential changes would help avoid the "heap out of memory" error and create more stable and efficient applications.

 See the Bull documentation for full details and other more advanced usage scenarios.

Ready to transform your business with our technology solutions?   Contact us today to Leverage Our Nodejs Expertise.

0

NodeJS

Related Center Of Excellence