Aximili
Aximili

Reputation: 29464

How do you increase NodeJS memory limit on Windows?

I need to run npm install with 8GB memory limit on NodeJS 14 on Windows 10 (with 16GB RAM).

I have tried the following to no avail:

enter image description here


Related questions:

Upvotes: 3

Views: 27779

Answers (2)

AKUMA no ONI
AKUMA no ONI

Reputation: 11819


TROUBLESHOOTING THE ISSUE


You haven't shown any troubleshooting results, thus far, that leads me to believe that the issue is being caused by the "HEAP's MAX_SIZE Env Variable", nor have you shown any evidence that the variable isn't being assigned the value that you are attempting to assign to it. I am not saying that the issue isn't being caused by the env Variable. However, I am saying, that more troubleshooting needs to be done to know for certain, what the underlying cause to your issue is..

Personally, from what you have shown the community thus far, I believe that the problem is due to running out of memory in a location other than the heap, which if your using a typical PC with Windows 10, its probably your machine that doesn't have enough memory, not period, but at the moment you attempt to do the download.



This is why I think you may be running out of memory:


In my machine I have:

  • 16GB of ram,
  • 4x HDD @ 2TB each for a total of 8TB of memory
  • a 8x Threaded Quad Core I7 6700 6gen from Intel
  • ASUS Motherboard w/ a Z190 Chip-set.

My Operating Systems

I use Linux, but I got Windows for Free because it was cheaper for me to buy a new PC, and upgrade it, than buy the parts seperatly. I never boot to windows, so its always sitting as an untouched fresh install.

Currently I just upgraded to Fedora Workstation 36.

  • HD #1 (2TB): Windows 10
  • HD #2 (2TB): Fedora Workstation
  • HD #3 & #4 (2TBx2 for 4TB total): Local Storage

TESTING

I ran performance test using a node program that implements child processes on other threads, and the Firefox Browser with up to 70 Tabs open, which I would attempt to refresh all at once.

The results were staggering.

I won't go into detail, but the important thing to note, is I have 16GB of DDR4 RAM

When Windows would need 15+GB of memory, Linux would only be using about 10GB+. That is a 37.5% - 42.5% gain.

However, I have Windows PRO, this allowed me to disable much of the telemetry, and I found that if I disabled windows telemetry and some other features windows was much more performant. When windows would use 15+ Linux would be at 12+ GB, that's still around a 25-20% gain of memory though.

One important thing to note though, sometime Linux would freeze up on me, windows was more robust at recovering when I overloaded the system, but it took quite a bit less to bog windows down.

The Point that I am making:

...is that your only using 8GB, that's not very much now'a days.

2BH I feel like the 16GB I have is far to little, I plan on upgrading soon.



STEP #1 — Troubleshooting the Issue Further


When troubleshooting, you need to always check that your syntax, and semantics are correct, that way you can rule out that the problem isn't due to a silly typo that you made.

"I know what your thinking..."
"Its not a typo!",

But as it turns out there is a typo: The code snippet you added as part of your question uses the Node Command-line Flag syntax, as the syntax for the Node Environment Variable, this is incorrect, as they use different sets of characters. At first glance, they look identical, but if you look at the 2-part MD table I created below, you'll see there is actually a pretty big difference between the two


              SEMANTICS                          SYNTAX                DIFFERENCE
Command-line Flag "--max-old-space-size" DASHES
Environment Variable "--max_old_space_size" UNDERSCORES
"For those with less-than-perfect vision, the difference is that the Environment Variable is typed using underscores, and the other is typed using dashes"


STEP #2 — TEST LOG "MAX-OLD-SPACE" POST-CONFIG


"If you used the right syntax and you still are having an issue, you can check to see that the max size of the heap is actually being configured to the size value that you are assigning to it. The process for logging the configured V8 Max Heap Size in the console can be done by completing the stops below."


TO START: Create a completely empty JavaScript file in an environment where you can run it using the node command — name the file test.js.

Add the code below to test.js, and don't add anything else.



/** 
  * @file "./test.js" 
  * @desc "PRINTS: The upper memory limit for V8s HEAP" 
  * */

const maxHeapSz = require('v8').getHeapStatistics().heap_size_limit;
const maxHeapSz_GB = (maxHeapSz / 1024 ** 3).toFixed(1);

console.log('--------------------------');
console.log(`${maxHeapSz_GB}GB`);




STEP #3 — Run the Following Commands




~/$ node --max-old-space-size=8192 test.js; node --max-old-space-size=4096 test.js; node --max-old-space-size=2048 test.js; node --max-old-space-size=1024 test.js; node --max-old-space-size=512 test.js

enter image description here

It would be very strange if you produced a different result than what the editor in the image (my editor) produced, and here is why:

The command above configures the "Max_Old_Space" Env Var. The program that I asked you to "Copy-&-Paste" into the file "test.js" prints the result of the commands Environment configuration change. It shows that every time you set the Env Var to something different, the Env Var is different during "Run Time". Its important to double note, its a variable, an "Environment Variable", it doesn't actually change the size of the heap, the value it represents is just saying that:

_"I the developer approve of the heap growing to the size that is set in the Environment Variable: "MAX_OLD_SPACE_SIZE"

...which does not instantly mean node is able to accomplish such sizes, hell, you could set it to 10e1000 MBs if you wanted, and it would mostly likely accept that number as a valid configuration. I tested all kinds of different stuff, and there is no doubt that you can set it several hundred times more than the amount of ram you actually have. In other words, its a variable, (i.e. an address to a location in memory that holds a binary value), there shouldn't be any issue setting it. It is far more likely that your machine is unable to allocate the amount of space needed, 8GB of RAM, when you only have 16GBs, can be, for some systems, a large amount to offer to a single process, especially f your running Windows, or MacOS. Linux is king in resource intensive situations.





STEP #4 — Lets See What Your System Has to Offer


"I assume your on Windows from the image you uploaded into your question, therefore, I will continue to answer this question for Windows only. If you need help with a different platform please let me know by editing your question."

Check your OS performance tool, see what the memory preformace chart looks like for your machine. If 8GBs aren't available, it won't matter how high you set the Heap Limit to. This is a very probable case, because you can set the HeapSize limit to use more ram that you physically have, and just because you got a 16MB stick doesn't mean half of it isn't being used, you might find that your machine is using 7-10Gbs for background processes. Windows is hard on system resources, that's why people use Linux/Unix for launching applications."_

Preform the Following Steps
  1. Press the following keys: CTRL + ALT + DEL
  2. Click on “Task Manager”
  3. Click on the “Performance” tab and check the section titled “Memory”
If the performance tool shows your that your Memory Use is above 45% then that is your problem. A quick solution to not having enough memory is to simply add more memory, this can be done by: Using additional sticks of Memory if your machine has room, upgrading to sticks with more memory, upgrading your entire machine, switching to a VM on the cloud with enough memory, upgrading your VM if your already using one, etc... There's an endless amount of different ways that one can go about about increasing memory, however; all of them will require money, therefore, it would be best to try and utilize your current memory to the best of your abilities, which would include some of the following options:
  1. Eliminating background processes
  2. Changing your platform to something less resource demanding. If you want to keep it GUI based, use Ubuntu 21.04, or Mint is a good alternative if your use to windows.
  3. if you really want to conserve as much resources as possible, spending the majority of your memory on your application/program/server, you will obviously want to use a command-line Interface, like Debian, Red-hat or Ubuntu, make sure to choose an LTS version, as it isn't like programming languages, using non LTS versions don't give you very many extra goodies, while LTS is extremely important to security.
  4. Lastly, this is obvious, but if it is possible, don't require downloads that need more memory than you can give.


"If this question still is not resolved at this point I will need a bit more info from you, such as the error messages you receive. Also a more in-depth explanation of what is actually happening, for instance, a further more indepth explanation about the statement below would be very helpful. As well as providing the screen shot asked for above"


What does this mean? What was unexpected?
"unexpected behavior but seems to still be 4GB after hitting CTRL + C"


Upvotes: 9

Foghorn
Foghorn

Reputation: 2326

The NODE_OPTIONS should work, especially in the environment variable. However, some of them should be underscores instead of hypens. Try --max_old_space_size=8192 in the NODE_OPTIONS environment variable instead. Here is a link to support that.

Upvotes: 1

Related Questions