node.js进程内存不足错误 [英] node.js process out of memory error

查看:176
本文介绍了node.js进程内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FATAL ERROR: CALL_AND_RETRY_2 Allocation Failed - process out of memory

我看到了此错误,但不确定从何而来.我正在处理的项目具有以下基本工作流程:

I'm seeing this error and not quite sure where it's coming from. The project I'm working on has this basic workflow:

  1. 从其他来源接收XML帖子
  2. 使用 xml2js
  3. 解析XML
  4. 从新创建的JSON对象中提取所需信息,然后创建一个新对象.
  5. 将该对象发送到连接的客户端(使用socket.io)
  1. Receive XML post from another source
  2. Parse the XML using xml2js
  3. Extract the required information from the newly created JSON object and create a new object.
  4. Send that object to connected clients (using socket.io)

正在使用的节点模块是:

Node Modules in use are:

  • xml2js
  • socket.io
  • choreographer
  • mysql

当我收到XML数据包时,我要做的第一件事是将其写入log.txt文件,以备日后需要查看某些内容时使用.我首先fs.readFile获取当前内容,然后编写新内容+旧内容.上次崩溃时,log.txt文件可能约为2400KB,但是重新启动服务器后,它又能正常工作,所以我不认为这是问题所在.

When I receive an XML packet the first thing I do is write it to a log.txt file in the event that something needs to be reviewed later. I first fs.readFile to get the current contents, then write the new contents + the old. The log.txt file was probably around 2400KB around last crash, but upon restarting the server it's working fine again so I don't believe this to be the issue.

在崩溃发生之前,我没有在日志中看到数据包,所以我不确定是什么原因导致崩溃...没有新的客户端连接,没有消息正在发送...没有任何内容被解析.

I don't see a packet in the log right before the crash happened, so I'm not sure what's causing the crash... No new clients connected, no messages were being sent... nothing was being parsed.

在我正在使用的每个对象达到其目的之后,我应该使用delete <object>来使节点持续运行,例如var now = new Date(),我可以将其与过去发生的事情进行比较.还是我将其传递给回调后的第3步中的结果对象?

Seeing as node is running constantly should I be using delete <object> after every object I'm using serves its purpose, such as var now = new Date() which I use to compare to things that happen in the past. Or, result object from step 3 after I've passed it to the callback?

如果有新客户端连接,我将保留一个主对象,它们需要查看过去的消息,尽管删除了对象,但它们在服务器的整个生命周期中都不会保留,直到它们在客户端完成为止.目前,我正在做类似的事情

I am keeping a master object in the event that a new client connects, they need to see past messages, objects are deleted though, they don't stay for the life of the server, just until their completed on client side. Currently, I'm doing something like this

function parsingFunction(callback) {
    //Construct Object
    callback(theConstructedObject);
}

parsingFunction(function (data) {
   masterObject[someIdentifier] = data;
});

编辑3

作为故障排除的另一个步骤,我在解析器开始于parser.on('end', function() {..});之前转储了process.memoryUsage().heapUsed并解析了多个xml数据包.在整个测试过程中,使用的最大堆约为10-12 MB,尽管在正常情况下程序的大小约为4-5 MB.我认为这不是特别破坏交易的行为,但可能有助于发现问题.

Edit 3

As another step for troubleshooting I dumped the process.memoryUsage().heapUsed right before the parser starts at the parser.on('end', function() {..}); and parsed several xml packets. The highest heap used was around 10-12 MB throughout the test, although during normal conditions the program rests at about 4-5 MB. I don't think this is particularly a deal breaker, but may help in finding the issue.

推荐答案

也许您不小心以递归方式关闭了对象.一个疯狂的例子:

Perhaps you are accidentally closing on objects recursively. A crazy example:

function f() {
  var shouldBeDeleted = function(x) { return x }

  return function g() { return shouldBeDeleted(shouldBeDeleted) }
}

要查找正在发生的情况,请启动节点检查器,并在可疑的内存不足错误之前设置一个断点.然后单击关闭"(在右边界附近的范围变量"下方).也许,如果您单击周围的内容,将会单击,您会意识到会发生什么.

To find what is happening fire up node-inspector and set a break point just before the suspected out of memory error. Then click on "Closure" (below Scope Variables near the right border). Perhaps if you click around something will click and you realize what happens.

这篇关于node.js进程内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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