Re: Best implementation of setTimeout / clearTimeout



bizt wrote:
I have a process where every so often I refresh a table's data using a
setTimeout() .. and when its no longer needed, a clearTimeout().

The following is a simple example of how this is done:

var goMenuTimeout = null; function LoadMenu() { // cancel any repeat
actions clearTimeout(goMenuTimeout);

// do some loading stuff . . .

// set timeout to refresh in 60 seconds goMenuTimeout =
setTimeout("LoadMenu()", 60000); }

This is just one example however my app has many setTimeouts to refresh
various elements. Now, the problem I may have is memory leaks and this is
one area I reacon it may be happening where objects created with
setTimeout are not being cancelled properly with clearTimeout() and over
time they are being retained in memory. Could this be the case even tho I
am using clearTimeout()?

Yes, objects created in code evaluated through window.setTimeout() are not
automatically garbage-collected when window.clearTimeout() is called.

I have also seen the following implementation:

clearTimeout(timeoutID); delete timeoutID;

This is how I have seen this process done on the MDC and some other
sites, will this reduce memory usage?

If `timeoutID' was *not* declared a variable (and so the created property
would lack the DontDelete attribute), then the additional `delete' operation
would probably save about 12 bytes of memory (32 bits [4 bytes] for the
variable pointer and 64 bits [8 bytes] for the IEEE-754 double-precision
floating-point value that it points to).

As far as I was aware clearTimeout() would destroy the repeating object
in the variable,

The return value of window.setTimeout() and the argument that
window.clearTimeout() expects is a number value that is the index of the
timeout/interval in the internal timeout/interval registry of the host
environment, not a direct reference to an object.

does it still get retained in memory?

If the aforementioned index specifies an object to implement the code to be
executed on timeout/interval click, then that object will allocate memory
until it is garbage-collected.

I have noticed that when I do alert(goMenuTimeout) each time it gives me
a different timeoutID value each time so I suspicious that perhaps these
timeout objects are getting retained and over time memory is being
allocated to redundant objects.

Although there is probably no timeout object as such, your suspicion is
warranted. The redundant objects would more likely to be the objects that
you create in the "do some loading stuff" part of the code instead.

Btw when I talk about memory leaks I meaning when I first load up
Firefox, in Windows Task Manager it is using 32KB of memory, but as

Probably you mean 32 _MiB_ of memory. I have yet to see a Firefox version
with an initial memory footprint lower than 20 MiB.

time goes on (ie. 20/30 minutes) it can be as high as 80/100KB with only
one tab open - in that time the app may have done one or two hundred
timeout processes. I have noticed once when the browser has been open for
a few hours that usage has risen to 400+KB which seems quite extreme

There are other factors to consider, though: browser version (2.0.x leaked
much more memory than 3.0.x does), add-ons and plugins that leak memory, and
so on. Most important is that new browser windows do not create new browser
processes, and therefore contribute to the total memory footprint of the
single browser process (you were emphasizing that you have only one tab open
in the window in question).


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
.



Relevant Pages

  • Re: infinite session timeout
    ... will remain in memory. ... closes their browser, then logs in again, you know will have two sessions on ... You should keep the timeout. ... It will be somewhat transparent for your users when their session does ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ADO.NET Timeout Exception - I have tried everything
    ... Check the memory usage/settings on the server. ... Im a creating a data set and populating it with a call to a store proc. ... Timeout expired. ...
    (microsoft.public.sqlserver.datawarehouse)
  • Best implementation of setTimeout / clearTimeout
    ... setTimeout() .. ... // set timeout to refresh in 60 seconds ... clearTimeoutand over time they are being retained in memory. ... each time so I suspicious that perhaps these timeout objects are ...
    (comp.lang.javascript)
  • Re: ((System.IO.Stream)(s)).ReadTimeout. What might be wrong?
    ... streams throw an exception. ... Setting the properties throws this exception, not a timeout during your read/write with the memory stream. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Session Time Out Problem
    ... It sounds like the ASP.NET application is restarting. ... I am facing a session Timeout problem on an ASP.NET project. ... memory.There is always at least 400 Mbytes of free memory on the system. ...
    (microsoft.public.dotnet.framework.aspnet)