Javascript Memory Leaks - setTimer() & setInterval() #127951
-
BodyI'm fairly new to Javascript and have just stumbled across the issue of memory leaks. As I've researched why my webpage may have crashed I discovered the memory leak is likely from the various setTimeout() functions and setInterval() functions that I have in my code. However, I am having a whole lot of trouble pinpointing this issue. I think the culprit is probably somewhere in the source code I've put below. There may be a few other spots where a setTimeout() or setInterval() function lives, but I think it is somewhere below. Can someone breakdown how I might be able to go about finding this memory leak? If you see something in my code (yes, it's probably pretty messy), please let me know. I'm going in circles at this point when it comes to identifying this memory leak. It seems to happen at a random time. P.S. - for context on what I am working on, this is a baseball game simulator. It is supposed to simulate a baseball game from start to finish. It has a pitch clock and is very dependent on the setIntervals and setTimers to keep the game flowing in a realistic manner. let intervalId;
let simInterval;
let throwpitchInterval;
var pitchInterval;
var pitchclockInterval;
var nextInterval;
function throwPitch() {
p = 1;
timeLeft = 1;
document.getElementById("pbp-text").innerHTML = "<b>The pitcher gets set...</b>";
pitchclock();
pitchTime();
throwpitchInterval = setTimeout(() => {
pitch();
atbatPitches++;
pitches++;
atbatRecord();
}, p);
throwpitchInterval = null;
if (batOver > 1000) {
document.getElementById("pbp-text").innerHTML = "<b>AT BAT OVER</b>";
resetCount();
clearInterval(pitchInterval);
};
}
function atbat() {
batOver = 0;
p = 1;
pitchclock();
pitchTime();
document.getElementById("pbp-text").innerHTML = `<b>Next up to bat: ${currentbatter.name}</b>`;
document.getElementById("lastpitch").innerHTML = "";
document.getElementById("velocity").innerHTML = "";
throwpitchInterval = setTimeout(() => {
pitch();
atbatPitches++;
pitches++;
atbatRecord();
}, p);
throwpitchInterval = null;
pitchInterval = setInterval(throwPitch, 15000);
}
function nextAtBat() {
showRunners();
setTimeout(() => {
resetCount();
atbat();
}, 10000);
}
function playball() {
document.getElementById("playball").style.display = "None";
document.getElementById("pbp-text").innerHTML = `<b>PLAY BALL!</b>`;
inning = 1;
switchSides();
setTimeout(() => {
atbat();
}, 6000);
}
function changeInning() {
if (inning == 1) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
/* document.querySelectorAll(".defense").style.backgroundColor="#BD3039";
document.querySelectorAll(".offense").style.backgroundColor="#ffffff";
document.querySelectorAll(".defense").style.border="2px solid #0D2B56";
document.querySelectorAll(".offense").style.border="2px solid #0C2340";
document.querySelectorAll(".defense").style.color="#ffffff";
document.querySelectorAll(".offense").style.color="#0C2340";*/
document.getElementById("box-T1").style.backgroundColor="#4f7e6f";
document.getElementById("box-B1").style.backgroundColor="#000000";
document.getElementById("bottom-1").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 1.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B1").style.backgroundColor="#4f7e6f";
document.getElementById("box-T2").style.backgroundColor="#000000";
document.getElementById("top-2").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 2) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-T2").style.backgroundColor="#4f7e6f";
document.getElementById("box-B2").style.backgroundColor="#000000";
document.getElementById("bottom-2").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 2.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B2").style.backgroundColor="#4f7e6f";
document.getElementById("box-T3").style.backgroundColor="#000000";
document.getElementById("top-3").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 3) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-T3").style.backgroundColor="#4f7e6f";
document.getElementById("box-B3").style.backgroundColor="#000000";
document.getElementById("bottom-3").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 3.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B3").style.backgroundColor="#4f7e6f";
document.getElementById("box-T4").style.backgroundColor="#000000";
document.getElementById("top-4").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 4) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-T4").style.backgroundColor="#4f7e6f";
document.getElementById("box-B4").style.backgroundColor="#000000";
document.getElementById("bottom-4").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 4.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B4").style.backgroundColor="#4f7e6f";
document.getElementById("box-T5").style.backgroundColor="#000000";
document.getElementById("top-5").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-T5").style.backgroundColor="#4f7e6f";
document.getElementById("box-B5").style.backgroundColor="#000000";
document.getElementById("bottom-5").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 5.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B5").style.backgroundColor="#4f7e6f";
document.getElementById("box-T6").style.backgroundColor="#000000";
document.getElementById("top-6").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 6) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
document.getElementById("box-T6").style.backgroundColor="#4f7e6f";
document.getElementById("box-B6").style.backgroundColor="#000000";
document.getElementById("bottom-6").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 6.5) {
inning = inning+0.5;
resetRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B6").style.backgroundColor="#4f7e6f";
document.getElementById("box-T7").style.backgroundColor="#000000";
document.getElementById("top-7").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 7) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
seventhinningstretch();
document.getElementById("box-T7").style.backgroundColor="#4f7e6f";
document.getElementById("box-B7").style.backgroundColor="#000000";
document.getElementById("bottom-7").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 7.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B7").style.backgroundColor="#4f7e6f";
document.getElementById("box-T8").style.backgroundColor="#000000";
document.getElementById("top-8").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 8) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-T8").style.backgroundColor="#4f7e6f";
document.getElementById("box-B8").style.backgroundColor="#000000";
document.getElementById("bottom-8").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning == 8.5) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
inningclock();
document.getElementById("box-B8").style.backgroundColor="#4f7e6f";
document.getElementById("box-T9").style.backgroundColor="#000000";
document.getElementById("top-9").innerHTML = inningRuns;
setTimeout(() => {
nextAtBat();
}, 15000);
} else if (inning >= 9) {
if (homeScore>awayScore) {
document.getElementById("pbp-text").innerHTML = "<b>GAME OVER</b>";
document.getElementById("bottom-9").innerHTML = 'X';
document.getElementById("box-T9").style.backgroundColor="#4f7e6f";
document.getElementById("box-B9").style.backgroundColor="#4f7e6f";
document.getElementById("away-runs").style.backgroundColor="#000000";
document.getElementById("away-hits").style.backgroundColor="#000000";
document.getElementById("away-errors").style.backgroundColor="#000000";
document.getElementById("home-runs").style.backgroundColor="#000000";
document.getElementById("home-hits").style.backgroundColor="#000000";
document.getElementById("home-errors").style.backgroundColor="#000000";
} else if (homeScore<awayScore) {
document.getElementById("pbp-text").innerHTML = "<b>GAME OVER</b>";
document.getElementById("box-T9").style.backgroundColor="#4f7e6f";
document.getElementById("box-B9").style.backgroundColor="#4f7e6f";
document.getElementById("away-runs").style.backgroundColor="#000000";
document.getElementById("away-hits").style.backgroundColor="#000000";
document.getElementById("away-errors").style.backgroundColor="#000000";
document.getElementById("home-runs").style.backgroundColor="#000000";
document.getElementById("home-hits").style.backgroundColor="#000000";
document.getElementById("home-errors").style.backgroundColor="#000000";
} else if (homeScore==awayScore) {
inning = inning+0.5;
resetRunners();
showRunners();
switchSides();
inningRuns = 0;
if (inning % 1 == 0) {
document.getElementById("last-inning").innerHTML = inning;
document.getElementById("box-T9").style.backgroundColor="#000000";
document.getElementById("box-B9").style.backgroundColor="#4f7e6f";
document.getElementById("top-9").innerHTML = inningRuns;
} else if (inning % 1 !== 0) {
document.getElementById("last-inning").innerHTML = (inning-0.5);
document.getElementById("box-T9").style.backgroundColor="#4f7e6f";
document.getElementById("box-B9").style.backgroundColor="#000000";
document.getElementById("bottom-9").innerHTML = inningRuns;
} else {
document.getElementById("last-inning").innerHTML = inning;
console.log('Error calculating extra inning.');
}
inningclock();
setTimeout(() => {
nextAtBat();
}, 15000);
};
} else {
document.getElementById("pbp-text").innerHTML = "<b>GAME OVER</b>";
document.getElementById("away-runs").style.backgroundColor="#000000";
document.getElementById("away-hits").style.backgroundColor="#000000";
document.getElementById("away-errors").style.backgroundColor="#000000";
document.getElementById("home-runs").style.backgroundColor="#000000";
document.getElementById("home-hits").style.backgroundColor="#000000";
document.getElementById("home-errors").style.backgroundColor="#000000";
};
}```
### Guidelines
- [X] I have read and understood this category's [guidelines](https://github.com/orgs/community/discussions/51384) before making this post. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @itsmichaelhagen , I think the memory leaks are caused by multiple intervals and timeouts instances running simultaneously, you need to clear them properly before setting new ones, try the following and check if it fixes it: First add this function to handle the clearing of timeouts and intervals at the top of your code, right after the variables. function clearInstances() {
if (throwpitchInterval) {
clearTimeout(throwpitchInterval);
throwpitchInterval = null;
}
if (pitchInterval) {
clearInterval(pitchInterval);
pitchInterval = null;
}
if (nextInterval) {
clearTimeout(nextInterval);
nextInterval = null;
}
} Then call the above function at the top inside clearInstances(); |
Beta Was this translation helpful? Give feedback.
Hi @itsmichaelhagen , wow that's a lot of global variables, I'm beginning to suspect that the problem you have isn't due to a memory leak but rather a general performance problem, while variables don't inherently cause a memory leak, having a lot of global variables can affect performance. You should limit their usage as much as possible and instead use local variables inside functions or blocks. Encapsulation comes in handy in such cases, encapsulating variables and related functions inside objects or modules to limit their scope and improve maintainability. Another thing that can help improve performance is to regularly clear or reset variables that contain lots of data which is the app…