简体   繁体   English

如何避免堆内存不足?

[英]How can I avoid heap out of memory?

I wrote this code to get ended my homework as a practice. 我写了这段代码是为了结束作业。

This code controls browser with nightmare.js.It just iterates clicking button and waiting for a sec. 这段代码使用nightmare.js控制浏览器,只是迭代单击按钮并等待一秒钟。

But this code issued an error of heap out of memory. 但是此代码发出了堆内存不足的错误。 I just tried out "--max-old-space-size=4096". 我刚刚尝试了“ --max-old-space-size = 4096”。 but it didn't work... ANYONE HELP ME?? 但这没用...有人帮助我吗?

I checked other than iterations can work. 我检查了迭代是否可以正常工作。 Then putting iterations..., it cannot work due to heap out of memory. 然后进行迭代...,由于内存不足,它无法工作。 To be honest, I am not good at coding and English. 老实说,我不擅长编码和英语。 If there are any miswriting, ask me please! 如果有任何误写,请问我!

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();

If I'm not mistaken, I think this is the reason. 如果我没记错的话,我认为这就是原因。

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"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM