What’s Heap Memory?
Before you start fixing the error, it’s a good idea to understand what heap memory is and how programs use it. Dynamically allocated memory is a memory that is allocated on the system heap. It is up to the programmer to use the available memory most. More crucially, the heap size of a program is determined by the amount of virtual memory allocated to it.
1 – Go to the Start menu, look for Advanced System Settings, and click the Best Match option.
2 – Select Environment Variables, then click New from System Variables or User Variables. The former affects all computer users, while the latter affects your current account.
3 – Node_OPTIONS should be entered in the Variable name box. Enter –max-old-space-size=4096 in the Variable value field. This number will give Node.js 4GB of virtual memory. To set a different number, multiply the required amount in GB by 1024 (the variable value must be in MB).
A Windows PowerShell terminal may also be used to set an environment variable.
Start a PowerShell terminal, run the following command, then hit Enter.
If you want to increase heap memory temporarily, run the following command in a PowerShell terminal before executing your project.
Once you’ve entered this command, you may use npm run dev or your script to deploy/run your project.
Always enter the needed memory size in megabytes (MB). Failure to do so may result in unexpected behavior in your program. It is also critical not to allocate all your available memory since this might result in a catastrophic system failure.
This will allocate 4GB of virtual memory to the Node.js execution area. Replace 4096 with the needed amount in MB to set a different amount of memory.
Add the above command to the configuration file to prevent repeating the process. To do so, use the same process as when you set your PATH variable.
You may avoid this error by ensuring your program is memory leak-free. When building programs in a low-level language, proper memory management is critical. While increasing the allocated memory would temporarily solve the problem, you should investigate and fix the underlying reason.