Memory leak problem if you don't call clearTimout after calling setTimeout

After calling setTimeout there a memory leak problem without calling clearTimeout ?

Thanks.

+7
javascript
source share
2 answers

Not. clearTimeout needs only to be called if you want to stop the waiting setTimeout. After setTimeout occurs, the timer identifier expires, but fortunately, calling clearTimeout with an invalid timer identifier is harmless.

If you see memory leaks, the problem occurs elsewhere.

+12
source share

There are times when setTimeout can cause a memory leak ... see the following article: setTimeout memory leaks

Be warned that IEx has the fineness of garbage collection; I think if you refer to the DOM variable in Javascript closure, then the collection mechanism gets confused, and at the end of the request it does not break, and in the end it becomes a memory leak. I think this is because the DOM variables and JS internal variables are collected by two different collectors, and they do not report correctly that they are no longer used.

I think you can fix this by setting the variable to null:

 setTimeout(function(){ myFunction(parameter); parameter = null }, myTimeout); 

This clearly sets the garbage collection in motion.

+6
source share

All Articles