简体   繁体   English

node.js进程内存不足错误

[英]node.js process out of memory error

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

Node Modules in use are: 使用的节点模块是:

  • xml2js xml2js
  • socket.io 套接字
  • choreographer 编舞
  • mysql MySQL的

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. 收到XML数据包时,我要做的第一件事是将其写入log.txt文件中,以防日后需要检查某些内容。 I first fs.readFile to get the current contents, then write the new contents + the old. 我首先使用fs.readFile获取当前内容,然后编写新内容+旧内容。 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. 上次崩溃时,log.txt文件可能约为2400KB,但是重新启动服务器后,它又能正常工作,所以我不认为这是问题所在。

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. 在崩溃发生之前,我没有在日志中看到数据包,所以我不确定是什么原因导致崩溃的。没有新的客户端连接,没有消息发送,没有任何解析。

Edit 编辑

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. 看到节点一直在运行,在我使用的每个对象达到其目的之后,我应该使用delete <object> ,例如var now = new Date() ,它用于与过去发生的事情进行比较。 Or, result object from step 3 after I've passed it to the callback? 或者,将其传递给回调后的第3步中的结果对象?

Edit 2 编辑2

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;
});

Edit 3 编辑3

As another step for troubleshooting I dumped the process.memoryUsage().heapUsed right before the parser starts at the parser.on('end', function() {..}); 作为故障排除的另一步骤,我在解析器从解析器开始之前就转储了process.memoryUsage().heapUsed parser.on('end', function() {..}); and parsed several xml packets. 并解析了几个xml数据包。 The highest heap used was around 10-12 MB throughout the test, although during normal conditions the program rests at about 4-5 MB. 在整个测试过程中,使用的最大堆约为10-12 MB,尽管在正常情况下程序的大小约为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. 也许如果您单击周围的内容,将会单击,您会意识到会发生什么。

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

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