Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there multiple node.js processes when I start my app on a single core machine

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.

htop screenshot process list

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.

htop process hierarchy

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.

like image 692
Bernhard Avatar asked Sep 18 '15 19:09

Bernhard


People also ask

How to run a NodeJS application on multiple CPU cores?

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.

What is multi-threading and multiple process in node?

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.

Can I run a cluster of NodeJS applications on a server?

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.

What is multiprocessing in Node JS?

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.


1 Answers

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.

like image 142
mmomtchev Avatar answered Sep 22 '22 13:09

mmomtchev