I'm running a 5k loc express application with a lot of additional dependencies including express. The application says it takes about 60 MB RAM RSS. But when looking at the actual RAM usage on my linux machine top tells me it uses ~1200 MB VIRT memory while taking ~60 MB RES (RSS) memory only.
I then wrote a simple test.js script to isolate the case:
setInterval(() => {
console.log(JSON.stringify(process.memoryUsage()));
} , 1000);
And started it using: node test.js
It tells me it uses around 30 MB RAM:
{"rss":30097408,"heapTotal":6537216,"heapUsed":3829224,"external":8272}
Looking at the memory in top command again, it shows me 30 MB RES, but around 470 MB VIRT usage.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8498 ted 20 0 485948 29928 23540 S 0,3 0,8 0:00.44 node
NodeJS doesn't seem lightweight this way.
The hoster of my NodeJS application (not the test app) grants me about 600 MB memory permanently for the application but kills my node process after some seconds due to high memory usage. The high virtual memory usage (the mentioned 1200/3800 MB) is taken as a trigger. So my questions are:
Why is the virtual memory usage for the application so high compared to the actual needed memory? What is the memory taken for?
Can I limit the virtual memory for NodeJS anyhow (is it used at all?)?