Node.js获得高内存使用率的通知 [英] Node.js get notification for high memory usage

查看:93
本文介绍了Node.js获得高内存使用率的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Amazon EC2上的Ubuntu 14上运行了一个Node.js应用程序。

I have a Node.js app running on Ubuntu 14 on Amazon EC2.

我希望在内存使用量达到特定大小的情况下发送电子邮件。

I want to send an email in case that the memory usage reaches a specific size.

我知道PM2公开了一个API,允许在达到一定量的内存使用量时重启应用程序。现在我不想重新启动应用程序,只是为了获得有关它的通知,并随意使用它(在我的情况下,发送电子邮件)。

I know that PM2 exposes an API that allows, among other things, restarting the app whenever a certain amount of memory usage is reached. Now I don't want to restart the app at this point, but just to get a notification about it, and doing with it whatever I want (in my case, sending an email).

如何使用PM2或任何其他免费工具?

How can I do it either with PM2 or any other free tool?

推荐答案

具体实现如下matthewmatician提到。

To have a concrete implementation as matthewmatician mentioned.


您可以通过定期检查内存使用情况在节点内轻松完成所有操作: https://nodejs.org/api/process.html#process_process_memoryusage 。如果这是一台长时间运行的服务器,请每隔一小时检查一次。如果内存达到一定的阈值,那么只需使用nodemailer这样的软件包启动电子邮件。

You can easily do this all within node by periodically checking your memory usage: https://nodejs.org/api/process.html#process_process_memoryusage. If this is a long-running server, check it every hour or so. Then just fire off an email with a package like nodemailer if the memory reaches a certain threshold.

我喜欢PM2,但我想知道如果可以在没有的情况下完成。

I like PM2, yet I was wondering if it could be done without.

此处所述, RSS(驻留集大小)是进程的完整内存使用情况,包括共享内存的所有内存使用情况。因此,可以将RSS视为不可用作为单个进程的内存使用,因为共享内存也被其他进程使用。因此PSS存在,它将共享内存划分为所有其他使用的进程。

As noted here, RSS (Resident Set Size) is the full memory usage of the process, this includes all memory usage of the shared memory. RSS can therefore be considered inacurate to be taken as the memory usage of a single process, since the shared memory is also used by other processes. Therefore PSS exists, which divides the shared memory over all other processes used.

我想如果我们想要显示最准确的内存使用情况,我们想知道PSS值由节点进程。但是,有人提到PSS对于大量的fork进程更为重要,例如Apache服务器(以及很多共享内存)。

I suppose we want to know the PSS value if we want to display the most accurate usage of memory by the node process. However, some person did mention PSS is more important for huge amount of fork processes, such as an Apache server (and thus a lot of shared memory).

var hour = 3600*1000;
var checkMemory = function() {
        // Retrieves the memory usage
        var memory = process.memoryUsage();
        var rss_memory_in_bytes = m.rss;
        var rss_memory_in_megabytes = m.rss / (1024**2);
        if (rss_memory_in_megabytes > 50) {
                // More than 50 megabytes RSS usage, do something
                doSomething();
        }
}
var doSomething = function() {
        // For instance sending an email
}
// Check the memory usage every hour
setInterval(checkMemory, hour);

- 更新 -

如果需要更多堆存储,Node进程将尝试分配此内存。这种分配是自动完成的。成功时,堆存储增加,rss也增加。因此,在我们的例子中可以忽略heapUsage和heapTotal。

If there is more heap storage required, the Node process will attempt to allocate this memory. This allocation is done automatically. When successfull the heap storage increases and the rss as well. The heapUsage and heapTotal can therefore be neglected in our case.

有一些设置内存限制的方法,但我们有兴趣检查限制。我认为检查剩余的可用系统内存量是合理的。然而,这与Node进程本身的实际内存使用量无关,并且需要对如何使用Node脚本检查空闲系统内存有不同的威胁。

There are ways of setting a memory limit, but we are interested at checking for a limit. I think it is reasonable to check for the amount of free system memory left. Yet, this has nothing to do with the actual memory usage of the Node process itself and would require a different threat on how to check for free system memory with a Node script.

这篇关于Node.js获得高内存使用率的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆