1

I'm running a node.js service on my Ubuntu 16.04 server. I came across a problem recently, where I get this error message: "UFATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory".

Now, I read here on SO that I can use something like: node --max-old-space-size=4096 yourFile.js to fix this, however I don't start my service like that, I use supervisorctl start <servicename> to start the service. So, I am not sure how can I solve th problem in my case. Can you suggest what should I do?

Keselme
  • 3,779
  • 7
  • 36
  • 68
  • It might be worth optimising the code rather than increasing the heap size. In the past I've had this issue when some recursive function gets out of control, perhaps you could start looking there? – Joseph Reeve Jan 24 '19 at 10:18
  • I actually don't have recursive code there, but I process client requests, and it can get up to hundreds requests per seconds – Keselme Jan 24 '19 at 10:21
  • At that point you really should be using a cluster of processes. This is where the first process accepts requests and dispatches them to workers. This is the same as preforking Apache. You get more heap space but you have to deal with the fact that you can't depend on having a single shared heap. – Dan D. Jan 24 '19 at 10:36

1 Answers1

2

supervisorctl use of a configuration file. It should be placed somewhere like :

/etc/supervisor/conf.d/myapi.conf

and look like :

[program:my-api]
command=node /home/myuser/myapi/app.js
autostart=true
autorestart=true
environment=NODE_ENV=production
stderr_logfile=/var/log/myapi.err.log
stdout_logfile=/var/log/myapi.out.log
user=myuser

add the running option inside of it :

[program:my-api]
command=node --max-old-space-size=4096 /home/myuser/myapi/app.js
autostart=true
autorestart=true
environment=NODE_ENV=production
stderr_logfile=/var/log/myapi.err.log
stdout_logfile=/var/log/myapi.out.log
user=myuser
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • 1. Is this solution depends on my node version? 2. Does it have default memory size to use? I use Grafana as a monitoring tool, and after looking at the timestamp of the crash I noticed that the memory usage as about 7 GB, which is very weird... – Keselme Jan 24 '19 at 10:30
  • As long as your node.js support the `max-old-space-size` option it will be fine. The default memory limit is 1GB [link](https://stackoverflow.com/questions/7193959/memory-limit-in-node-js-and-chrome-v8). Idk about your 7GB there must be something else than the node.js HEAP SIZE which is what is limited to 1GB. – Orelsanpls Jan 24 '19 at 10:42