Situation
I'm actually analyzing my node.js application on my server and local machine for memory optimization and found something which is confusing.
If I run my app locally (mac osx) and see a single process in the systems activity monitor — That's what I expect because there are no child processes launched with e.g. cluster.
But when I run the app on my staging maching (AWS Ubuntu EC2 Micro Instance) I see via htop
there are five related processes running my app instead of the expected one process.
Analysis
Each process uses exact the same memory.
After I had a look into the processes parents htop
showed up, there's one node parent with four children.
I've tried to kill one child process which cause a complete crash (when running without pm2) so it seems each process is required.
Question
Why is there a difference between the operating systems and why does node obviously need multiple processes on the ubuntu system.
Thanks for any help understanding it.
Luckily, Node.js has a core module named Cluster that will help us run a Node.js application using all the CPU cores on a machine. In this article, our Node.js application will be an ExpressJS HTTP server that we'll create a unique instance for on each CPU core.
Multi-Threading and Multiple Process in Node.js - Flatlogic Blog What is Node.js? Multi-Threading and Multiple Process in Node.js What is Node.js? NodeJS is a free-to-use, cross-platform JavaScript runtime environment that although is single-threaded in nature but uses multiple threads in the background for executing asynchronous code.
A single instance of a Node.js application runs on only one thread and, therefore, doesn't take full advantage of multi-core systems. There will be times when you may want to launch a cluster of Node.js processes to utilize each CPU core on a local machine or production server.
Multiprocessing is a server-side scaling technique in Node that allows you to have more than one Node instance running, ideally one for each of your processor cores.
Node has only a single process but it has lots of threads. The standard top
including the OS X process monitor displays processes. By default htop
displays threads. You can switch the display mode by pressing H
.
V8 has its own threads - including the GC, Node can have its own threads in some cases, libuv launches UV_THREADPOOL_SIZE
threads - these are the threads for the async I/O. There are lots of threads in a Node executable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With