【问题标题】:Memory usage with Node.js, Jsdom, HttpAgentNode.js、Jsdom、HttpAgent 的内存使用情况
【发布时间】:2013-01-28 20:32:17
【问题描述】:

我制作了一个浏览博客以获取所有标题的报废脚本。问题是随着脚本运行(数千个 URL),Node 一直使用越来越多的内存,直到 8 go(最大),然后脚本崩溃。

我的脚本使用循环,一定有简单的方法来清除内存?

这是一个代码示例:

var request = require('request'),
httpAgent = require('http-agent'),
jsdom = require('jsdom').jsdom,
myWindow = jsdom().createWindow(),
$ = require('jquery'),
jq = require('jquery').create(),
jQuery = require('jquery').create(myWindow),
profiler = require('v8-profiler');

profiler.startProfiling();

request({ uri:'http://www.guylabbe.ca' }, function (error, response, body) {
  if (error && response.statusCode !== 200) {
    console.log('Error when contacting URL')
  }


        var last_page_lk = $(body).find('.pane-content .pager li:last-child a').attr('href');
        var nb_pages = last_page_lk.substring(last_page_lk.indexOf('=')+1);
        var page_lk_base = last_page_lk.substring(0,last_page_lk.indexOf('='));

        var pages = Array();
        pages.push(page_lk_base);
        for(var i=1;i<=nb_pages;i++) {
            pages.push(page_lk_base+'='+i);
        }


        // parser les pages

        var fiches = Array();
        var agent2 = httpAgent.create('www.guylabbe.ca', pages);

        agent2.addListener('next', function (err, agent2) {

            var snapshot = profiler.takeSnapshot();


            $(body).find('.view span.field-content span.views-field-title').each(function(){
                fiches.push($(body).find(this).parents('a').attr('href'));
                //console.log($(body).find(this).html());
            });


            agent2.next();

        });
        agent2.start();

        agent2.addListener('stop', function (agent) {
          console.log('-------------------------------- (fini de cumuler les URL fiches) --------------------------------');

            // Parser les fiches

            var agent_fiches = httpAgent.create('www.guylabbe.ca', fiches);

            agent_fiches.addListener('next', function (err, agent_fiches) {

                console.log('log info');


                agent_fiches.next();

            });
            agent_fiches.start();

            agent_fiches.addListener('stop', function (agent) {
              console.log('-------------------------------- Eh voilà! --------------------------------');
            });

            agent_fiches.addListener('start', function (agent) {
              console.log('-------------------------------- C est parti... --------------------------------');
            });

        });



});

【问题讨论】:

  • 你能分享更多的上下文,一些代码吗?你用的是什么库?
  • 您知道您发布的代码越相关,答案就越准确?
  • 对不起,我添加了代码示例

标签: javascript node.js memory


【解决方案1】:

在不再需要它们的地方显式地为空变量。如果您在闭包之外创建变量,并在闭包内使用它,那么当您不再需要它时应该将其设为空。看到这个线程并阅读接受的答案:How to prevent memory leaks in node.js?

【讨论】:

    【解决方案2】:

    我遇到了类似的 jsdom 内存泄漏问题。 在我的情况下,通过解决它来关闭 jsdom 窗口。 也许你应该在完成抓取后添加myWindow.close()。 查看相关答案https://stackoverflow.com/a/6891729/1824928

    【讨论】:

    • 我觉得这应该有所帮助,但我是 Node.js 的新手,而且我的代码编写方式,我无法完成这项工作。非常感谢您的意见!
    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 2013-11-29
    • 2012-03-29
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多