如何避免堆内存不足?

时间:2018-07-29 15:13:28

标签: javascript heap-memory

我写了这段代码是为了结束我的家庭作业。

此代码使用nightmare.js控制浏览器。只是迭代单击按钮并等待一秒钟。

但是此代码发出了内存不足的堆错误。我刚刚尝试了“ --max-old-space-size = 4096”。但这没用... 有人可以帮助我吗?

我检查了迭代是否可以工作。然后进行迭代...,由于内存不足,它无法工作。 老实说,我不擅长编码和英语。如果有任何误写,请问我!

const Nightmare = require('nightmare');
const nightmare = Nightmare({show: true});


const LinguaURL = "";
const NumberOfMine = "";
const PwdOfMine = "";

var i,j = -1;
var selection, progress;
var radioSelect = -1;

function main() {
  nightmare
    .goto(LinguaURL)
    .wait(1000)
    .type("input[type='text']", NumberOfMine)
    .type("input[type='password']", PwdOfMine)
    .click("input[type='submit']")
    .wait(5000)
    .click('a[href="javascript:document.Study.submit()"]')
    .wait(3000)
    .click("input[type='button']")
    .wait(3000);

    for(i = 3;i<43;i++){
      nightmare
        .click('a[onclick="unit_view_page(\''+i.toString()+'\');"]');
        while(true){
          j++;

          if(j % 4 == 0){
          nightmare
            .click('input[onclick="select_unit(\'drill\', \''+(1833+j).toString()+'\', \'\');"]')
            .wait(1000);

          while(true){
            radioSelect++;

            nightmare
              .click('input[id="answer_0_' + radioSelect.toString() +'"]')
              .wait(1000)
              .click('input[id="ans_submit"]')
              .wait(1000)
              .evaluate(function (){
                 return selection = document.querySelector('.btn btn-answer-view form-font-size');
              })
              .evaluate(function (){
                return progress = document.querySelector('btn btn-next-problem form-font-size');
              });

              if(selection == null && progress == null)break;

              if(selection != null){
                continue;
              }else{
                nightmare
                  .click('input[class="btn btn-next-problem form-font-size"]');
                continue;
              }
          }
          if((j + 1) % 10 == 0)break;
          }
        }
      }
      nightmare
        .wait(100)
        .end()
        .then(console.log);
}

main();

1 个答案:

答案 0 :(得分:0)

如果我没记错的话,我认为这就是原因。

function main() {
    for(i = 3;i<43;i++){
        while(true){
            j++;
            if(j % 4 == 0){
                while(true){
                    /**** break for the second while. ****/
                    if(selection == null && progress == null) break;
                    if(selection != null){
                        continue;
                    }else{
                        continue;
                    }
                }
                /**** break for the first while. ****/
                // But this code will be run if 'j % 4 == 0',
                // and I can't see any assigned the value to 'j' variable inside of 'if'. 
                // There's no way the result will be true.
                if((j + 1) % 10 == 0) { break; }
            }
        }
    }
}

var j = -1;
var limit = 2000 * 4;
while(true){
    j++;
    if(j != 0 && j % 1000 == 0) console.log("loop: " + j);
    // here we go.
    if(j % 4 == 0){
        while(true){
            break;
        }
        // But this code will be run if 'j % 4 == 0',
        // and I can't see any assigned the value to 'j' variable inside of 'if'. 
        // There's no way the result will be true.
        if((j + 1) % 10 == 0) { console.log("your if statement"); break; }
        // limit
        if (j == limit) { console.log("break using limit"); break; }
    }
    // Maybe (do you just want to run ten times?)
    // if((j + 1) % 10 == 0) { console.log("[maybe] your if statement"); break; }
}
console.log("process end");