Search code examples
node.jsmemorymemory-managementswapswapfile

Can a Nodejs process uses more memory than the available physical memory (by using swap memory)?


I am using Nodejs. I am planning to increase the memory limit of a Nodejs application.

While googling, I found this article: Increasing Node.js memory limits.

The author says his server only has 8GB of physical memory, but his Nodejs process is using 28GB of memory. I assume that it is using physical + swap memory. The article also mentioned that the developer of a famous Nodejs framework uses 15GB memory limit for his Nodejs.

I have tried to search for some examples from other programming languages. In Java, it seems that it is not a good idea to use swap memory for JVM heap. Referring to this article: How increasing swap size allow me to increase the heap size?, using swap memory for JVM heap will create big problems.

I am not sure whether there is any difference between the JVM GC and the Nodejs GC.

Can a Nodejs process uses more memory than the available physical memory (by using swap memory)?


Solution

  • Can a Nodejs process uses more memory than the available physical memory (by using swap memory)?

    Yes, it can. Like any other process. It is managed by the OS. The process asks for a new page and it doesn't even know if it's swapped or not.

    I wouldn't recommend it, though, as it can lead to degraded performance. Disks (even SSD) are orders of magnitude slower than RAM. Personally I don't like using any swap memory at all on my systems for exactly that reason. Unfortunately on Linux it means that I can't suspend to disk but I can live with that.

    What you actually see when you check the memory usage is not always that obvious. Some shared libraries can be mapped to several processes at the same time and the usage you see is not always real. Also a process can ask for a lot of memory but it doesn't mean it gets it. The real physical memory (RAM or swap) can be mapped when a given page is actually accessed for the first time, not when it is requested. It's done with page faults.

    For more info see: