简体   繁体   English

node.js 堆内存和使用的堆大小 [pm2]

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

I am currently running node.js using pm2.我目前正在使用 pm2 运行 node.js。

And recently, I was able to check "custom metrics" using the pm2 monit command.最近,我能够使用 pm2 monit 命令检查“自定义指标”。

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.实际上,我检查了在不同服务器上运行的 pm2。

Each was set to 95mib / 55mib, and accordingly, the used heap size was different.每个都设置为 95mib / 55mib,因此,使用的堆大小不同。

Also, is the heap usage closer to 100% the better?另外,堆使用率是否越接近 100% 越好?

While searching on "StackOverflow" to find related information, I saw the following article.在“StackOverflow”上搜索相关信息时,看到了下面这篇文章。

What does Heap Usage mean in PM2 堆使用在 PM2 中意味着什么

Also what means active requests ?还有什么意味着主动请求? It is continuously zero.它连续为零。

Thank you!谢谢!


[Edit] [编辑]

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

node version : v10.15节点版本:v10.15

[Additional] [额外的]

server memory : 1GB [ 40~50% used ]服务器内存:1GB [已使用 40~50%]

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

The heap is the RAM used by the program you're asking PM2 to manage and monitor.堆是您要求 PM2 管理和监视的程序使用的 RAM。 Heap space, in Javascript and similar language runtimes, is allocated when your program creates objects and released upon garbage collection.在 Javascript 和类似语言运行时中,堆空间在您的程序创建对象时分配并在垃圾收集时释放。 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.例如,nodejs/express web 服务器为每个传入的请求分配reqres对象,然后使用它们,然后删除它们,以便垃圾收集器可以回收它们的 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.活动请求计算通过各种异步对象(如文件编写器和 TCP 连接)完成的工作。 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.如果它爬起来,一些计算功能正在占用 Javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM