Search code examples
javascriptajaxmemory-leakswindows-desktop-gadgets

Question about memory leaks Javascript Windows Gadget (Also in general)


I was working on memory leaks for a Windows 7 Gadget for awhile, and then got more functionality to implement which I believe made the memory leaks worse/added more leaks. I ran 4 instances of the gadget for roughly 16 hours and memory usage went from 22,000K to 36,000K. However, something that perturbed me is that the Handles, USER Objects, and GDI Objects remained roughly the same. The USER and GDI Objects actually remained exactly the same and the Handles jumped from something like 1026 to 1034 in all that time. I consider that to just be some eccentricity of sidebar since each of my gadgets refresh every 7 seconds.

I have two questions, in the general case, what kind of memory leaks cause something like this? And in my case I use Ajax to access web services for this gadget, so if anyone knows what might cause such a thing in Ajax that would be great.

I have already seen articles on main causes of memory leaks in Javascript, I'm just wondering if anyone has any ideas on why the memory would go up so much when the handles and objects stayed effectively the same. Perhaps there are known causes in Ajax or programming in general for such a case?


Solution

  • I definitely appreciate JS work done without libraries but in a situation like this, you may want to toss JQuery (or Prototype, Mootols, etc) into the mix. They are very well tested for memory consumption with Ajax requests (and DOM manipulation).

    You don't need to modify your DOM code at first but try replacing your Ajax requests with JQuery's Ajax methods. Then run your memory tests again. If that eliminates it, you know you've got a memory bug in your XHR code.

    Otherwise, its probably related to event handlers in your DOM code not being properly removed before you destroy an element (I'm assuming you remove and add DOM elements at some point). If a handler isn't removed in IE, that will create a memory leak (the DOM element is held in memory indefinitely, suck).