Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js heap memory and used heap size [pm2]

I am currently running node.js using pm2.

And recently, I was able to check "custom metrics" using the pm2 monit command.

Here, information such as Heap size, used heap size, and active requests are shown.

I don't know how the heap size is determined. Actually, I checked pm2 running on different servers.

Each was set to 95mib / 55mib, and accordingly, the used heap size was different.

Also, is the heap usage closer to 100% the better?

While searching on "StackOverflow" to find related information, I saw the following article.

What does Heap Usage mean in PM2

Also what means active requests ? It is continuously zero.

Thank you!


[Edit]

env : ubuntu18.04 [ ec2 - t3.micro ]

node version : v10.15

[Additional]

server memory : 1GB [ 40~50% used ]

cpu : vCPU (2) [ 1~2% used ]

like image 893
hyundeock Avatar asked Oct 08 '20 06:10

hyundeock


People also ask

What is heap size in PM2?

The heap is the RAM used by the program you're asking PM2 to manage and monitor. Heap space, in Javascript and similar language runtimes, is allocated when your program creates objects and released upon garbage collection.

How much memory does PM2 use?

Monitoring With PM2 The default memory consumption is about 50M for the maintenance and 70M for the homepage project. That's all the magic. Keymetrics (the developers of PM2) uses this feature to report the current process utilization directly to their dashboard which is accessible via web interface.

How do I check Nodejs memory usage?

memoryUsage() method is an inbuilt method of the process module that provides information about the current processes or runtime of a Node. js program. The memory usage method returns an object describing the memory usage in bytes of the Node.

What is Nodejs heap size?

By default, Node. js (up to 11. x ) uses a maximum heap size of 700MB and 1400MB on 32-bit and 64-bit platforms, respectively.


1 Answers

The heap is the RAM used by the program you're asking PM2 to manage and monitor. Heap space, in Javascript and similar language runtimes, is allocated when your program creates objects and released upon garbage collection. Your runtime asks your OS for more heap space whenever it needs it: when active allocations exceed the free space. So your heap size will probably grow as your program starts up. That's normal.

Most programs allocate and release lots of objects as they do their work, so you should not try to optimize the % usage of your heap. When your program is running at a steady state – that is, after it has started up — you'll find the % utilization creeping up until garbage collection happens, and then dropping back. For example, a nodejs/express web server allocates req and res objects for each incoming request, then uses them, then drops them so the garbage collector can reclaim their RAM.

If your allocated heap size keeps growing, over minutes or hours, you probably have a memory leak. That is a programming bug: a problem you should do your best to solve. You should look up how that works for your application language. Other than that, don't worry too much about heap usage.

Active requests count work being done via various asynchronous objects like file writers and TCP connections. Unless your program is very busy it stays near zero.

Keep an eye on loop delay if your program does computations. If it creeps up, some computation function is hogging Javascript.

like image 164
O. Jones Avatar answered Sep 24 '22 07:09

O. Jones