JavaScript Heap Out of Memory Error: Causes & How to Fix

Reason behind this error could be insufficient space allocated

Reading time icon 3 min. read

Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

Key notes

  • The reason behind this could be an invalid page table size or issues with function call stack.
  • This guide will discuss troubleshooting steps to fix the error.
JavaScript Heap Out of Memory Error Causes & How to Fix

If you have encountered the JavaScript heap out-of-memory error, this guide can help! This often happens if the application runs out of memory. We will cover all the available fixes right after discussing the causes of the same. Let’s start! 

What causes the JavaScript heap out of memory error? 

There could be various reasons why the project crashed and presented you with this error; some of the common ones are mentioned here: 

  • Large objects present – If the code has a lot of large objects, then memory consumed by those objects collectively leads to an overload, hence the error.
  • Insufficient amount of memory allocated – If the default amount allocated by Node.js is insufficient to complete a command, you might encounter this error.  
  • Memory leaks – As the app grows, it is important to delete some objects you don’t need; if not done, you might get this error. 
  • Large data structures – If you work with large data structures such as arrays or matrices, it can consume a lot of space, causing this error. Additionally, when working with arrays, ensuring efficient use of array methods is crucial. Learn more about fixing Expects a Return Value in JavaScript to optimize your code and prevent memory issues.
  • Infinite loops – Infinite loops use memory pretty fast, and it might cause this error.

What can I do to fix the JavaScript heap out of memory error? 

1. Use the Control Panel 

  1. Press Windows + E to open Windows Explorer.
  2. Go to This PC, right-click it, and select Properties.Properties javaScript Heap Out of Memory Error
  3. From the next page, select Advanced system settings.Advanced system settings javaScript Heap Out of Memory Error
  4. Switch to the Advanced tab, and click Environment VariablesEnvironment Variables
  5. Click New on either System variables (changes apply to the entire computer)  or User variables ( applies the changes to a user account).New
  6. On the next window, for the Variable name: enter Node_Options.
  7. Now for Variable value: –max-old-space-size=4096Click OK javaScript Heap Out of Memory Error
  8. Click OK. You can change 4096 to 8192 as well to allocate more space.
  9. On the Environment Variable window, click OK.
  10. Now click Apply and OK to save the changes. 

2. Use PowerShell

  1. Press the Windows key, type PowerShell, and click Run as administrator. Powershell 2 javaScript Heap Out of Memory Error
  2. Copy and paste the following command and press Enter: env:NODE_OPTIONS="--max-old-space-size=4096"
  3. To temporarily solve the issue, you can type the following command and press Enter before opening the app: set NODE_OPTIONS=--max-old-space-size=4096

So, these are methods to fix the JavaScript heap out-of-memory error. To avoid this error in the future, it is advised to split your data into smaller chunks and delete the objects that are no longer needed. Try them and let us know what worked for you in the comments section below. 

More about the topics: Javascript