A
A
Alexander2014-08-28 14:22:19
Node.js
Alexander, 2014-08-28 14:22:19

How to avoid memory leak in nodejs?

I am writing a spider on nodejs, I am faced with the fact that it constantly eats more and more memory. I tried to make it as simple as possible, this is what happened:

var humanize = require('humanize')
  , request  = require('request');
  
function fetch(url, c) {
  request(url, function(err, resp, body) {
        if (err) {
            c(err);
            return;
        }
        c(err, resp, body)
    });
}  

function callback(err, resp, body) {
    var memory = process.memoryUsage();
    console.log("Memory usage: " + humanize.filesize(memory.heapUsed) + " / " + humanize.filesize(memory.heapTotal));
    fetch('http://google.com', callback);
}

fetch('http://google.com', callback);

After launch, 8.57 MB / 17.77 MB is used, and after 20-30 requests it is already 13.09 MB / 45.71 MB.
Why is this happening and how to avoid increasing memory consumption?
PS I tried to write through events, essentially the same callbacks, but just in case. The result is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2014-08-28
@Taraflex

habrahabr.ru/post/123154
read paragraph
node --nouse-idle-notification --expose-gc server.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question