Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 383
A Wizard Did It Avatar asked Nov 26 '10 17:11

A Wizard Did It


1 Answers

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.

like image 58
nalply Avatar answered Sep 20 '22 16:09

nalply