Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Ajax memory leak using setTimeout

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've modeled some code after the final refactored "net.ContentLoader" in Chapter 9 of "Ajax In Action" for a "Status Application" that I'm building for use within a closed network. I'm seeing a bad slowdown in Mozilla 1.7 and Firefox 2.0 for Sun Solaris 10 using prstat; CPU usage goes to 10~15% and stays there while the browser slows to a crawl after a couple hours with the main page open. I also see a nasty memory leak accumulate ~500K at a time using XP's task manager through Netscape 7.1. These leaks total up to hundreds of MBs eventually. I also have no need to support IE, so most, if not all of the results of google searches have produced no help.

I'll include my js Ajax object and the specific code from ContentLoader causing the leak; specifically the onreadystatechange anonymous function closure) below. The catch to the accumulation of the leak is that I am using setTimeout to call the initialize function once every second. I need to do this in order to display the latest status of the following on my main page: 9 "red/green" status fields, 5 dynamic text fields with status colors, and a large table's html text being built in the php get script of the XHR request. I have several other pages in my site that use Ajax, but this main page is the best example since it has the most updating going on.

Note: I had to modify the exact examples from the book to get the Ajax updating to work initially. I am open to trying anything, but the use of oThis remained necessary in order to capture the context/scope of the code so that the callback can reference that context/scope. I have tracked the leak down to the use of oThis/ in the closure with the "Javascript Memory Validator" tool but have not found a workaround.

Thanks for any help!
~Cory

 
Cory Lancaster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One quick extra note:

I made some changes to the code last night to move the "new XHR()" object creation to within the ContentLoader's "constructor" call so that I only get one XHR object per item being updated on my main page and the page still leaks like a sieve.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you positive the leak is in the Ajax code and not in the code that you are using when the response comes back?

Eric
 
Cory Lancaster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the javascript that does the dynamic updating of whatever emements I'm changing the statuses of?

I've tried setting any and all vars declared to null. I've also tested with a very simple div that I'm filling with the current time fetched from MySQL on the server and I see it still leak, albeit a lot less, but still consistently on each update.

Update, I'll post the updating code example for the big table:


[ May 01, 2007: Message edited by: Cory Lancaster ]
 
It's exactly the same and completely different as this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic