Posts Tagged ‘setTimeout memory leak’

setTimeout memory leak

Sunday, March 20th, 2011

For those who use setTimeout function in Javascript - do not pass name of function you want to trigger as a sting. If used with string - Javascript will use EVAL to execute it - this is creating a memory leak if used for instant loop. Garbadge collector will not clear the memory after calling clearTimeout and delete setTimeout object. Insted of using string - use blank funtion and put funtion you want to trigger inside. This will not involve EVAL, but will generate a pointer that Garbadge collector will handle and free up memory.

// generates memory leak:
myTimeout = setTimeout(”myFunction()”, 1000);
// no memory leak (no parameters to pass):
myTimeout = setTimeout(myFunction, 1000);
// OR:
myTimeout = setTimeout(function() { myFunction(); }, 1000);

Unfortunately, all the the samples of setTimeout function out there in the internet are passing string as parameter. Hard to identify memory leak, but it is there.

школа современных танцев в голосеевском районе