首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何避免堆内存不足?

如何避免堆内存不足?
EN

Stack Overflow用户
提问于 2018-07-29 23:13:28
回答 1查看 570关注 0票数 1

我写这段代码是为了把我的作业作为练习来结束。

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

但是这段代码发出了堆内存不足的错误。我刚试过"--max-old-space-size=4096“。但它并没有起作用。有人来帮我吗??

我检查了迭代以外的其他工作。然后放迭代...,它不能工作,因为堆内存不足。老实说,我不擅长编码和英语。如果有写错的地方,请找我!

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 08:54:50

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

代码语言:javascript
复制
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; }
            }
        }
    }
}

代码语言:javascript
复制
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");

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51581699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档