Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js using JSON.stringify results in 'process out of memory' error

Tags:

json

node.js

ldap

With Node I am trying to collect user data from an LDAP server and then write that data to a JSON file. I am using the following code to do this:

fs.writeFile('data.json', JSON.stringify(data, null, 4));

The problem is the JSON.stringify method is causing the following error:

FATAL ERROR: JS Allocation failed - process out of memory

I know the problem is with JSON.stringify because if I use console.log rather than fs.writeFile I get the same error.

I am trying to write a lot of data (over 500 entries in the LDAP database). Does anyone know how I can get this to work? Here is the code in full:

var ldap = require('ldapjs');
var util = require('util');
var fs = require('fs');
var client = ldap.createClient({
  url: '************'
});

client.bind('CN=**********,OU=Users,OU=LBi UK,OU=UK,DC=********,DC=local', '*********', function(err) {
  if (err) {
    console.log(err.name);
  }
});


// taken from http://ldapjs.org/client.html
client.search('OU=Users,OU=******,OU=UK,DC=******,DC=local', {
  scope: 'sub',
  filter: 'objectClass=organizationalPerson',
  attributes: ['givenName', 'dn', 'sn', 'title', 'department', 'thumbnailPhoto', 'manager']
  // filter by organizational person
}, function(err, res) {
  if (err) {
    console.log(err.name);
  }

  var limit = 1;
  var data = {"directory": []};

  res.on('searchEntry', function(entry) {

    var obj = {};
    entry.attributes.forEach(function (attribute) {
      var value;
      if (attribute.type === 'thumbnailPhoto') {
        value = attribute.buffers[0];

      } else {
        value = attribute.vals[0];
      }
      obj[attribute.type] = value;
    });
    data.directory.push(obj);
  });
  res.on('error', function(err) {
    console.log('error: ' + err.message);
  });
  res.on('end', function(result) {
    fs.writeFile('data.json', JSON.stringify(data, null, 4));
  });

});
like image 305
Stephen Young Avatar asked Jun 25 '12 13:06

Stephen Young


People also ask

Can JSON Stringify throw an error?

Error Handling parse(), JSON. stringify() may throw errors, so it should be wrapped in a try catch statement. The function throws a TypeError in two contexts: if a circular reference occurs in the Javascript object or if the Javascript object contains a BigInt. A replacer cannot catch or change these errors.

What is the output of JSON Stringify?

Stringify a JavaScript Object const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

What is JSON Stringify in node JS?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

What is the time complexity of JSON Stringify?

It's considered O(n) as simple grammar doesn't require even lookaheads.


1 Answers

As @freakish mentioned the problem was my data was too big.

The reason the data was so big was due to a large number of images that were being returned as objects. In the end all I needed to do was encode the object as base64 using Buffers and then the size of the data became much more manageable.

like image 55
Stephen Young Avatar answered Oct 26 '22 19:10

Stephen Young