JavaScript - Memory Leak??
Hi everyone,
Really hoping someone can help me with a javascript / memory problem I'm having. Environment is Win7, MSIE v8.0. I have a pretty simple test function for this problem: Code: function go() { // create giant text string in variable 'bill' then set 'bill' to null - store inside variable called 'test' as text. var test = "bill = 'blah blah blah blah blah (repeat blah about a thousand times or so for file size)'; bill = null;"; // execute text inside variable 'test' as code var tester = new Function(test); // destroy function used to execute string as code tester = null; } now, when you trigger the function go() through a button (ie <button onclick='go();'>go</button>) you can watch the memory usage in taskmgr go up every time you click the button (for me it goes up about 3MB every time go() is called - but that will depend on how many blah's you have). I have tried replacing var tester = new Function(test); with the eval() command... Same problem. It would appear the function is creating (storing in memory) a new instance every time its called ... What am I doing wrong here?? How do I stop the memory increasing every time i try to execute the string 'test' as script? Checked this code in Chrome and Firefox - they don't seem to have any memory issues running this code - its only a IE issue (*sigh*). That doesnt help me as the problem relates to a client's HTA application (can't switch off IE engine). Huge thanks in advance! Similar TutorialsI'm working on a Virtual Tabletop project and am running into issues where tons of memory is accumulated. I've set up a test where 502 pieces are on the board and every second the board randomly zooms in/out 5%. After 30 seconds, I clear the interval. http://ontodevelopment.com/boardtest/ When the board zooms, every game piece has has to be re-sized. Using Firefox, watch the memory go up but never come back down. In IE and Chrome the leak is no where near as bad. Any ideas or pointers would be appreciated. Hi every1! Got a task in university to make a memory game (card matching game). Thats what i've got so far: Code: var selected = 0; var choiceOne; var choiceTwo; var matches = 0; var numOfTries = 0; var interval = 700; var gamePlace = document.getElementById("wrap"); var backcard="img/memoryBg.png"; var timeStart; var timeFinish; var time; function nameEnter(){ var name = prompt("Please enter your name"); if (name != null){ document.title = "Welcome " + name + "!"; } } function game(){ var nOfPairs = document.getElementById("nPairs").value; timeStart = new Date().getTime(); if (nOfPairs==1){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; } if (nOfPairs==2){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==3){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==4){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==5){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==6){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards.sort(function shuffle() { return Math.random() - 0.5;}); } if (nOfPairs==7){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards[12] = 'img/card7.png'; cards[13] = 'img/card7.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==8){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards[12] = 'img/card7.png'; cards[13] = 'img/card7.png'; cards[14] = 'img/card8.png'; cards[15] = 'img/card8.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if(nOfPairs>8||nOfPairs<1){ alert("Please enter the number from 1 to 8"); }else{ for(i = 0; i<=nOfPairs*2-1; i++){ var cardDiv = document.createElement("div"); cardDiv.setAttribute("id",""+i+""); cardDiv.style.height="65px"; cardDiv.style.width="65px"; cardDiv.style.float='left'; gamePlace.appendChild(cardDiv); document.getElementById(""+i+"").innerHTML='<img src="img/memoryBg.png">'; cardDiv.onclick=function(){cardClick(this.id);}; } } function cardClick(card){ if (selected==2){ return ; } if (selected==0){ choiceOne=card; document.images[card].src = cards[card]; selected = 1; }else { selected = 2; choiceTwo = card; document.images[card].src =cards[card]; var timer=setInterval(checking,interval); } function checking() { clearInterval(timer); numOfTries++; document.getElementById("tries").innerHTML = numOfTries; if (cards[choiceTwo]==cards[choiceOne]){ matches++; document.images[choiceOne].src = "img/memoryBgI.png"; document.images[choiceTwo].src = "img/memoryBgI.png"; //choiceTwo.onclick=null; } else { document.images[choiceOne].src = "img/memoryBg.png"; document.images[choiceTwo].src = "img/memoryBg.png"; selected = 0; return ; } if (matches == nOfPairs){ timeFinish = new Date().getTime(); time = Math.round (((timeFinish - timeStart)/1000)); alert("Congrats! You needed " + numOfTries + " tries, and " + time + " seconds."); } selected = 0; return ; } } } so the problem is, that I can just click 2 times on one card and it will look like a coincidence. My second problem is that when I have a real coincidence, I can still click on that card (by "real coincidence" i mean two same pics. so when i find them, they have to be not active, and when im clicking on them, they should just simply do nothing.). Can someone help me? Okay, so I'm making something so IF the number is BETWEEN this number, and that number, I want it to display a certain number. Ex. Code: var Num1 = 100 var Num2 = 200 var Num3 = 1 If (Num1{THIS IS WHAT I DON'T KNOW}Num2) { alert(Num3) } Also, could somebody please tell me what those are called? The; == != || etc. Because I was having a TERRIBLE time trying to google that. AHH! I'm sorry for this. I know this is a stupid question. I've created a web application which uses just JS\Jquery\ajax, without any page reloads. The issue is, it starts working really slow after using it for a while since it probably has lots of memory leaks. No i'm reading about memory consumption and memory leaks and my first question is about the following scenario. I have an ajax request each second which checks for new messages. and when i get the response, the following loop is part of the code. Code: for (i=0; i<pm_length; i++) { var content = data.pm[i].content; var mood = data.pm[i].mood; var time_added = data.pm[i].time_added; var fb_id = data.pm[i].user_id; var msg_id = data.pm[i].id; var user_status = data.pm[i].user_status; var full_name = data.pm[i].first_name + " " + data.pm[i].last_name; if($("#pm"+fb_id).length == 0) { var closetab = '<a class="close">x</a>'+full_name; $(".tabs").append('<li class="bot_room" id="pm'+fb_id+'" type="pm" tab="#tabpm'+fb_id+'">'+closetab+'</li>'); $(".tab_container").append('<div id="tabpm'+fb_id+'" class="tab_content" style="display: none;"></div>'); } create_message('pm'+fb_id, msg_id, fb_id, full_name, content, 'no', user_status, time_added, mood); if($("#pm_alert").attr('checked')) { soundManager.onready(function(){ var mySound = soundManager.createSound({ id:'mySound1', url:'images/new_msg.mp3' }); mySound.play(); mySound = null; }); } content = null; mood = null; time_added = null; fb_id = null; msg_id = null; user_status = null; full_name = null; } and my question is, is there a reason to actually null the variables if they will be redeclared any way on the following second? Greeting....! Does anyone know that how's the JavaScript memory model work? I mean as we know that a Stack stores primitive type variables and references, a Heap stores referenced type object, what about functions? Should functions are stored at a Memory or what I said is wrong. Is there any books or resources are talking about JavaScript Memory Model? Thanks a lot!!!! Dan Hi: I was trying to post in flowplayer forum but... Anyway, the code below works fine when run with browser called from Coffee Cup HTML editor. Files are played as requested from button onClick events. All files called and all ref files are on hosting server. When I put this page on the server and click the first button, the files play fine. When I then click thge second button, the files called by the first button play again. If I then click the second button, the files called by button 2 play. If I then click button 5, the files called by button 2 play again. The previous config is somehow being stored somewhere and called. I have added this and that as you can see in the code to try to rid the old config but to no avail: playerConfig = null playerConfig.playlist =[]; should clear it. delete playerConfigplaylist[0]; Should clear it. and thinking it was bumped to [1] delete playerConfigplaylist[1]; Should clear that. So I think the old playerConfig.playlist should be gone but apparently not so. I have reviewed: jquery.min.js, flowplayer-3.2.6.min.js and flowplayer.playlist-3.0.8.js and cannot see any cause to this behavior though I am not a js ace. Any insight appreciated. Best regards <code> <script> var mediaPlayer = null; var playerConfig = null; function createPlayerConfig () { playerConfig = null; playerConfig = { log: { level: 'debug' } , play: null, plugins: { controls: { url: 'https://www.jala-mi.org/gmfiles/flowplayer.controls-3.2.5.swf', bottom: 0, height: 24, backgroundColor: "#2d3e46", backgroundGradient: "low", fontColor: '#ffffff', timeFontColor: '#333333', autoHide: 'never', play:true, volume:true, mute:true, time:true, stop:true, playlist:true, fullscreen:true, scrubber: true } } } }; function changePlaylist(newplaylist,no_of_items) { playerConfig.playlist = []; delete playerConfig.playlist[0]; for(i=0; i < no_of_items; i++) { playerConfig.playlist[i] = newplaylist; } delete playerConfig.playlist[1]; createPlayer(); // reload player with new playlist mediaPlayer.play(); // start the newly loaded clip sequence* } function createPlayer() { if(mediaPlayer != null) { if(mediaPlayer.isLoaded()) { mediaPlayer.unload(); mediaPlayer = null; } } $f("player", { src:"https://www.jala-mi.org/gmfiles/flowplayer-3.2.7.swf" }, playerConfig ); mediaPlayer = $f("player"); } function loadandplay(intro, movie) { var presentation = {url: movie , duration: 0, autoPlay: true, autoBuffering: false, fadeInSpeed: 8000, fadeOutSpeed: 5000, title: 'JPD 02/28/2011',playlist: [{url: intro , duration: 0, position: 0, fadeInSpeed: 5000, fadeOutSpeed: 8000, autoPlay: true, autoBuffering: true, }] }; createPlayerConfig(); changePlaylist(presentation, 1); }; </script> </code> html <code> <div id="side"> <br><br> <input type="button" class="plbtn" value="November 2010" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro112210.flv', 'https://www.jala-mi.org/gmfiles/collections112210.flv')"> <br> <input type="button" class="plbtn" value="January 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="February 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro22811.flv', 'https://www.jala-mi.org/gmfiles/jpd_3.flv')"> <br> <input type="button" class="plbtn" value="March 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro32811.flv', 'https://www.jala-mi.org/gmfiles/assets32811.flv')"> <br> <input type="button" class="plbtn" value="April 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="May 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="September 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="October 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="November 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> </div> </code> I am using firefox 8. Is there a way is seeing the details of its memory usage please? Many thanks for any help, S Doing some RSA encryiption in java script Nothing big, strings like max 20 chars in lenght. After a couple (random, could be 10 or 50 or anything operations) of that, browser does not freeze, server does not freeze but wont work anymore on any string, i.e same result for any string. Restarting browser helps. What should I look for, are there any tools for FF to look for js memory leaks an such ? I'm designing a page to fade multiple images (three) in and out over a set period of time (one minute, say). The code I have works as follows: I define an array and fill it with the div objects I want to fade in and out. I have some simple tags (2 for opaque, 1 for fading in, -1 for fading out, etc.) that I set to show the state of the object. First I check if the object is opaque, and if so set it to fade along with a time; Or if it's transparent I give it a 1 in 10 chance of fading in during this iteration. If it's currently fading in or out I check to make sure it's on track and change the opacity of the div accordingly. Originally, I had the function call back to itself using setTimeout after the loop had gone through all the divs and changed their opacities appropriately. This caused an out of memory error (though, strangely, if I had any alerts in the function it didn't give me the error). So the version below has the code in a while loop to let it run for 20 seconds - this gives me the "a script...run slowly, do you want to continue the script?" message. So I have several questions: 1) First and foremost, why is my function causing out of memory/run slowly errors and what is the best way to fix that? 2) Is there a better way to code this effect (final result will be six lights fading in and out for a minute or so). 3)I'm not an experienced programmer, so any bad practices, ways to streamline, no no's, etc. that you see, please point out. I've only posted the script here - the page simple consists of three divs (id's grad0, grad1, grad2) and a call to the function copied here. Thanks in advance. Code: function commencez(){ //parent function to store var's var TimeToFade = 10000; theArray = new Array(2); var wrench = new Date().getTime(); for (var a=0; a<3; a++){ //fill the array with objects theArray[a] = document.getElementById("grad" + a); theArray[a].FadeState = -2; //start them transparent } function fader() { var check = new Date().getTime(); //stops the function after 30 seconds if( check <= wrench + 30000 ) { return; } for(var i=0; i<3; i++){ if (theArray[i].FadeState !=1 && theArray[i].FadeState != -1) { if( theArray[i].FadeState == 2) { theArray[i].FadeState = -1; theArray[i].FadeTimeLeft = TimeToFade; theArray[i].FadeBegan = new Date().getTime(); } if( theArray[i].FadeState == -2) { var rndm=Math.floor(Math.random()*11); if( rndm == 1) { theArray[i].FadeState = 1; theArray[i].FadeTimeLeft = TimeToFade; theArray[i].FadeBegan = new Date().getTime(); } } } if( theArray[i].FadeState == 1 || theArray[i].FadeState == -1) { var now = new Date().getTime(); since = now - theArray[i].FadeBegan; if( theArray[i].FadeTimeLeft <= since ) { theArray[i].style.opacity = theArray[i].FadeState == 1 ? '1' : '0.2'; theArray[i].style.filter = 'alpha(opacity = ' + (theArray[i].FadeState == 1 ? '100' : '20') + ')'; theArray[i].FadeState = theArray[i].FadeState == 1 ? 2 : 2; } theArray[i].FadeTimeLeft -= since; var howMuch = theArray[i].FadeTimeLeft / TimeToFade; if( theArray[i].FadeState == 1) { howMuch = 1-howMuch; theArray[i].style.opacity = howMuch; theArray[i].style.filter = 'alpha(opacity = ' + (howMuch*100) + ')'; } if( theArray[i].FadeState == -1) { theArray[i].style.opacity = howMuch; theArray[i].style.filter = 'alpha(opacity = ' + (howMuch*100) + ')'; } } } //close the for loop setTimeout( fader() , 500); //last thing fader does is recall itself after half a second } //close the fader function fader(); } //close parent function </script> ok, i'm sure you all will laugh at this, but it seems really bizzario to me. i have two small routines worked out. routine G gets a value out of the db and brings it back to the browser script. routine P puts a value into the db from the browser script. they both run fine. but......... you have to turn the browser totally off and restart it after retrieving a value out of the db (?) why? is this some kind of "clear memory" i need to be doing. its like the last value i retrieved thru the server is stuck in memory and keeps coming up, and it won't flush out until i turn the browser off and restart it. is this a Javascript problem or a PHP problem? i dont' have a clue, maybe i need to somehow flush the memory in the PHP file after i echo() back the result.? is that normal? thanks, Paul |