5

I am using https://www.npmjs.com/package/memory-usage to see how my Node application uses the memory, by running 'memory-usage app.js' in MACOS terminal. However, after some testing, it always crash at 1.4GB.

In normal nodejs command, to set the memory limit higher, I could just use 'node --max-old-space-size=8192 app.js'. However running 'memory-usage --max-old-space-size=8192 app.js' won't work.

My Mac has following RAM details:

BANK 0/DIMM0:

Size: 4 GB Type: DDR3 Speed: 1867 MHz

BANK 1/DIMM0:

Size: 4 GB Type: DDR3 Speed: 1867 MHz

What is the maximum size I can go for my Nodejs application and how to set that with memory-usage?

Thank you.

ZachB
  • 13,051
  • 4
  • 61
  • 89
cristie09
  • 311
  • 1
  • 3
  • 11
  • 2
    May be worth trying to run the following command from within your project directory: `node --max-old-space-size=8192 /usr/local/lib/node_modules/memory-usage/cli.js app.js` - _Note: In this command we invoke the cli.js file for `memory-usage` instead of the actual command itself._ – RobC Dec 29 '18 at 18:24
  • @robC thanks! this works! although i have to manually stop my app because it is scary when it reaches 5.5GB on my laptop – cristie09 Dec 30 '18 at 08:42
  • I was getting this error while running a NestJS App in a docker container. I increased the memeory allocated to Docker to 3 GB and the build was successful – Harshit Gangwar Dec 12 '21 at 11:15

2 Answers2

6

Try setting it via NODE_OPTIONS:

Unix:

export NODE_OPTIONS="--max-old-space-size=8192" && memory-usage app.js

Windows:

set NODE_OPTIONS="--max-old-space-size=8192" && memory-usage app.js
willascend
  • 1,503
  • 8
  • 15
  • thanks! I tried but it doesn't work. The command mentioned by robC in the comment above works. – cristie09 Dec 30 '18 at 08:43
  • Hi @cristie09, glad you got it to work. What OS are you on? If Windows, you might need to use `set NODE_OPTIONS...` – willascend Dec 30 '18 at 13:25
  • 1
    oh, i'm using MacOS? – cristie09 Dec 30 '18 at 16:15
  • Got it. So you night need to include`export` in your command. I've updated my answer to reflect that. Also, this is a good post concerning what value you should use: https://stackoverflow.com/a/48392705/2920706 – willascend Dec 30 '18 at 20:17
  • Should it be with underscores or dashes? Should it have quotes around or not? How do I actually check if nodejs picked the NODE_OPTIONS I set? – JustAMartin Nov 29 '22 at 14:02
  • hey @JustAMartin, use dashes. https://nodejs.org/api/cli.html#useful-v8-options. If you need more help outside of this, I suggest you create a new question or do a search to see if it has been asked before. – willascend Nov 29 '22 at 16:08
  • @willascend Thank you. I was confused because I have seen both options with dashes and with underscores in different answers, and some claim that you have to use dashes for command line arguments, but underscores for the environment variable. The answers on the web can be so inconsistent sometimes... – JustAMartin Nov 29 '22 at 16:18
-1

Its likely that the memory-usage executable is just ignoring your max-old-space argument. You should be able to find a way around that in their docs. It looks like Node has default memory allocation of 1G on 64bit systems.

See related (possibly duplicate) thread. Limiting node.js's memory usage

1.4G sounds a bit high to me. I wonder if you're leaking memory somewhere?

See also: How to monitor the memory usage of Node.js?

4m1r
  • 12,234
  • 9
  • 46
  • 58
  • hi, thankyou for the links. Yes it is ignoring, the argument and hence I have used the command mentioned above by robC. – cristie09 Dec 30 '18 at 09:26
  • My memory usage is big due to big data taken from database converted to json to be sent to client. I'm finding out ways to perhaps not pulling so much data now or sending them in smaller packets. Do you know if max-old-space = RSS? – cristie09 Dec 30 '18 at 09:35