node.js heap memory and used heap size [pm2]
Asked Answered
S

1

7

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 ]

Scagliola answered 8/10, 2020 at 6:25 Comment(0)
E
13

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.

Eba answered 10/10, 2020 at 15:42 Comment(1)
Thanks for the explained details, is there are tools to check the source of the memory leak?Unskilled

© 2022 - 2024 — McMap. All rights reserved.