node.js process out of memory error

喜你入骨 提交于 2020-01-03 08:09:10

问题


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
  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

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.

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. Or, result object from step 3 after I've passed it to the callback?

Edit 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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/4287494/node-js-process-out-of-memory-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!