欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NodeJS入门翻车现场:FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

程序员文章站 2022-06-01 09:29:59
...

 代码:

var request = require('request');
var cheerio = require('cheerio');

var articlelisturl = 'https://blog.csdn.net/funkstill/article/list/';

function getArticleLink(starturl,callback){

    var articlelinkList=[];//文章列表

    //遍历所有文章列表页面
    var pagenum =0;
    var flag = true;
    while(flag){
        //拼接文章列表页面链接
        articlelisturl=articlelisturl+pagenum.toString();
        //获取页面
        request(articlelisturl,function(err,res){
            if(err){
                callback(err);
            }
            
            var $ = cheerio.load(res.body.toString());
            if($('div').hasClass('article-list')){//如果列表非空
                var articleList = $('#article-item-box csdn-tracking-statistics');
                articleList.each(function(item){
                    var artList = $(this);
                    var articleLink = artList.find('h4').find('a').attr('href');
                    articlelinkList.push(articleLink);
                })
            }else{//如果列表为空
                flag = false;
            }
            
            
        });
        pagenum++;
    }
    callback(null,articlelinkList);
}
getArticleLink(articlelisturl,function(err,articlelinkList){
    if(err){
        return console.log(err);
    }
    console.log(articlelinkList);
});

问题: 

PS G:\NodeJS\demo\CSDNSpider> node CSDNSpider.js

<--- Last few GCs --->

   50565 ms: Scavenge 1355.2 (1458.1) -> 1355.2 (1458.1) MB, 0.5 / 0 ms [allocation failure].
   50568 ms: Scavenge 1355.3 (1458.1) -> 1355.3 (1458.1) MB, 0.5 / 0 ms [allocation failure].
   50569 ms: Scavenge 1355.3 (1458.1) -> 1355.3 (1458.1) MB, 0.5 / 0 ms [allocation failure].
   50642 ms: Mark-sweep 1355.3 (1458.1) -> 1355.3 (1458.1) MB, 73.2 / 0 ms [last resort gc].
   50715 ms: Mark-sweep 1355.3 (1458.1) -> 1355.3 (1458.1) MB, 72.8 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 000002AF74DE3AD1 <JS Object>
    1: parse [url.js:~71] [pc=000001DD448EEB1F] (this=0000027968AFD851 <an Url with map 00000227167BA2F9>,url=0000027968AFD829 <Very long string[113970]>,parseQueryString=000002AF74D04189 <undefined>,slashesDenoteHost=000002AF74D04189 <undefined>)
    2: parse(aka urlParse) [url.js:~63] [pc=000001DD4726C7BF] (this=000000529B9E0B59 <an Object with map 0000022716745FF9>,url=0000027968AFD829 <Ve...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

解决方案: