Memory leaks PhantomJS内存泄漏和进程退出失败

Memory leaks PhantomJS内存泄漏和进程退出失败,memory-leaks,phantomjs,Memory Leaks,Phantomjs,我目前正在使用PhantomJS进行一个项目,该项目评估CSV文件指定的网页列表。我安装了NPM和node.js以在我的程序中使用 节目如下: var async = require("async"); var webpage = require('webpage'), fs = require('fs'); var file_h = fs.open('C:\\Users\\morgan\\Documents\\FantasyApp\\URLPlayerListActive.txt',

我目前正在使用PhantomJS进行一个项目,该项目评估CSV文件指定的网页列表。我安装了NPM和node.js以在我的程序中使用

节目如下:

var async = require("async");
var webpage = require('webpage'),
    fs = require('fs');

var file_h = fs.open('C:\\Users\\morgan\\Documents\\FantasyApp\\URLPlayerListActive.txt', 'r');
var urls = [];
while (!file_h.atEnd()) {
    urls.push(file_h.readLine());
}

async.eachSeries(urls, function (url, done) {
    console.log(url)
    var page = webpage.create();
    page.open("http://"+url, function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
            console.log(status)
            var closeresults = page.close();
        } else {
            var evalresults = page.evaluate(function() {
                try {
                    table2csv('pgl_basic');
                    try {
                        ga('send','event','Tool','Action','CSV');
                    }
                    catch (e) {}
                    var list = document.querySelectorAll('#csv_pgl_basic');
                    var stats = [];
                    for (var i = 0; i < list.length; i++) {
                        stats.push(list[i].innerText);
                    }
                    return stats;
                    var closeresults = page.close();
                } catch (e) {
                    console.log(e);
                }
            });
            try {
                fs.write("C:\\Users\\morgan\\Documents\\FantasyApp\\Data\\"+url+".txt",     evalresults.join('\n'), 'w');
                var closeresults = page.close();
            } catch(e) {
                console.log(e);
                var closeresults = page.close();
            }
        }
        done();
    });
});
phantom.exit();
var async=require(“async”);
var webpage=require('webpage'),
fs=要求('fs');
var file_h=fs.open('C:\\Users\\morgan\\Documents\\FantasyApp\\urlpayerlistactive.txt','r');
var url=[];
而(!file_h.atEnd()){
push(file_h.readLine());
}
eachSeries(url,函数(url,完成){
console.log(url)
var page=webpage.create();
页面打开(“http://”+url,函数(状态){
如果(状态!=“成功”){
console.log('无法访问网络');
console.log(状态)
var closeresults=page.close();
}否则{
var evalresults=page.evaluate(函数(){
试一试{
表2CSV(“pgl_基本”);
试一试{
ga('send'、'event'、'Tool'、'Action'、'CSV');
}
捕获(e){}
var list=document.querySelectorAll('csv#u pgl_basic');
var stats=[];
对于(变量i=0;i
我的症状是进程内存增加,直到达到我的Windows最大值并崩溃,或者它完成了我的列表,进程永远挂起

我可以为这两个问题中的任何一个实现解决方案,但因为它们都发生了,所以我无法让这个脚本工作


我正在寻找帮助,以防止内存泄漏,或者在脚本完成后简单地关闭我的进程。这些症状可能来自相同的根本原因。

如果页面没有正确地进行垃圾收集,您可以反复尝试使用同一实例。另一件事是,您应该在脚本实际完成时调用
phantom.exit
,例如在
eachSeries
的回调中

var page = webpage.create();
async.eachSeries(urls, function (url, done) {
    console.log(url)
    page.open("http://"+url, function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
            console.log(status)
        } else {
            var evalresults = page.evaluate(function() {
                try {
                    table2csv('pgl_basic');
                    try {
                        ga('send','event','Tool','Action','CSV');
                    }
                    catch (e) {}
                    var list = document.querySelectorAll('#csv_pgl_basic');
                    var stats = [];
                    for (var i = 0; i < list.length; i++) {
                        stats.push(list[i].innerText);
                    }
                    return stats;
                } catch (e) {
                    console.log(e);
                }
            });
            try {
                fs.write("C:\\Users\\morgan\\Documents\\FantasyApp\\Data\\"+url+".txt",     evalresults.join('\n'), 'w');
            } catch(e) {
                console.log(e);
            }
        }
        done();
    });
}, function(err){
    phantom.exit();
});
var page=webpage.create();
eachSeries(url,函数(url,完成){
console.log(url)
页面打开(“http://”+url,函数(状态){
如果(状态!=“成功”){
console.log('无法访问网络');
console.log(状态)
}否则{
var evalresults=page.evaluate(函数(){
试一试{
表2CSV(“pgl_基本”);
试一试{
ga('send'、'event'、'Tool'、'Action'、'CSV');
}
捕获(e){}
var list=document.querySelectorAll('csv#u pgl_basic');
var stats=[];
对于(变量i=0;i
其他一些问题:

  • page.close
    不返回任何内容,因此
    closeresults
    将是
    未定义的
  • return
    之后的任何语句都无法执行
  • page
    未在页面上下文中定义(在
    page.evaluate
    内),因此
    page.close()产生一个可能会破坏代码的错误
请注册到和事件以查看是否存在错误