为什么这种延迟的 WHILE 循环会导致大量内存泄漏? [英] Why is this delayed WHILE loop causing a massive memory leak?

查看:100
本文介绍了为什么这种延迟的 WHILE 循环会导致大量内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从本网站的其他地方翻录了一个不错的技巧,可以让您延迟循环的周期.我在 Chrome 版本 34.0.1847.131 m 中使用在 TamperMonkey 上运行的 Javascript bot 对其进行了一些修改以供自己使用.

I ripped a nice trick from somewhere else on this site that allows you to delay the cycles of a loop. I modified it a little for my own use with a Javascript bot that runs on TamperMonkey, in Chrome Version 34.0.1847.131 m.

无论出于何种原因,一旦调用循环,就会开始发生大量内存泄漏(大约每秒 40,000K - 80,000K)并且它根本不循环.我的电脑已经崩溃了两次,因为我既没有准备也没有预料到它.

For whatever reason, as soon as the loop is called, a MASSIVE memory leak begins to occur (at about 40,000K - 80,000K per sec) and it doesn't loop at all. My computer already crashed twice because I was neither prepared for nor expecting it.

 //Tests if Bot is still connected
var connected = true;

function testConnection() {    //  create a loop function
   setTimeout(function () {    //  call a 3s setTimeout when the loop is called
                                //  your code here
      randQuery = "PING " + Math.floor((Math.random()*8234));  //Creates a value of "PING " plus a random number attatched to the end to ensure that window.find only matches with the new ping 
    
      document.getElementById('msg').value = randQuery; //Inputs the above-created value into the message box
      DoMessage();                                     //Submits what's in the message box. Fails if m.xat.com no longer has a connection to the chatroom.
      var connected = window.find(randQuery);  //If the above failed, randQuery will not be found, returning the value of "False". There's probably a better solution for this?
   
        if (connected == true) {
            sendMessage("succeeded"); //DEBUG
        }
        else {
            sendMessage("failed");  //DEBUG
            connected = false;
            Reb00t();              //Reloads the page if DoMessage() failed
        }
       
       
      while (connected == true) {    //  if bot is still connected, call the loop function
         testConnection();          //  ..  again which will trigger another 
      }                            //  ..  setTimeout()
   }, 9000)
}

setTimeout( function startPingLoop() {
    testConnection();                      //  start the loop
}
,1200);

我在这里搞砸了什么吗?我以前从未处理过内存泄漏.

Is there something I screwed up here? I've never dealt with a memory leak before.

推荐答案

只要 connected 等于 true,while 循环就会一遍又一遍地触发,中间没有延迟.while 循环不是异步的 - 它会暂停脚本中发生的所有其他事情,直到它结束.

As long as connected is equal to true, the while loop will keep firing over and over, with no delay in between. A while loop is not asynchronous - it will pause everything else going on in the script until it is over.

把循环想象成一个自动代码编写器.它编写了一个函数,直到括号中的语句为假为止.

Think of a loop like an automatic code writer. It writes a function that goes for as long as until the statement in the brackets is false.

  while (connected == true) {
     testConnection();
  }

相当于写作:

  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
...

一遍又一遍,只要connected 等于true.因此,如果 connected 保持等于 true,while 循环会尝试编写无限长的代码,从而导致无限循环,直到 testConnection() 函数使 connected 等于 false.如果您的计算机有无限内存,那么这个无限长的代码将占用所有内存 - 但您的计算机没有无限内存供应,因此它会崩溃.

over and over again for as long as connected is equal to true. So if connected stays equal to true, the while loop tries to write an infinitely long code, causing an infinite loop until the testConnection() function makes connected equal to false. If your computer had infinite memory, this infinitely long code would take up all of it - but your computer doesn't have an infinite supply of memory, and so it crashes.

这篇关于为什么这种延迟的 WHILE 循环会导致大量内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆