How do you increase NodeJS memory limit on Windows?

0

Issue

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:

  • node --max-old-space-size=8192 "C:\Program Files\nodejs\npm" install – unexpected behaviour but seems to still be 4GB after Ctrl+C-ing
  • set NODE_OPTIONS=--max-old-space-size=8192&&npm install – still 4GB
  • Adding NODE_OPTIONS environment variable (to both User and System variables) – still 4GB
    enter image description here

Related questions:

Solution


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.


Here is why I think you are running out of memory:


"I also have 16GB of ram, in my machine with a 6Gen I7. My machine has 3 HDDs, 1 2TB that I use for storage, a single TB HDD for running the platform that I use for development "Ubuntu 21.04", and another single 1TB HDD for "Windows 10 Pro Edition". I tested the 2 against each other for the sake of this post, and I did write a long excerpt, but for the sake of brevity, I am going to just tell you the results, if you have question, you can ask in the comments. Running my only me editor with a few tabs open (5-tabs), and a browser with several tabs open, as I usually do (9-tabs). Ubuntu used 22.8% of 16.2GB of Memory, and having the same software opened in windows, but using windows default browser EDGE, rather than Ubuntu’s default Firefox browser, Windows used 38.2%. Now, edge was faster, because it doesn’t have the browser extensions that I downloaded and use with Firefox, it also doesn’t have a bunch of cached data to continuously track, and update yet, as I never used it before. Windows also isn’t backing up my files, as I never use windows so it is a fresh install. Ubuntu has many processes running for little things I use. In the end it is easy to see how asking for 8GB’s from a machine running windows can easily be over doing it. But the only way to know for certain is to trouble shoot the issue, which I have laid out how to do below"

STEP #1 — SYNTAX & SEMANTICS


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`);


  1. 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 #3 — 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"

Answered By – JΛYÐΞV

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More