[wp-hackers] JS memory leaks

Adam Batkin adam at batkin.net
Tue Oct 3 11:26:34 GMT 2006


 > Perhaps someone a bit more savvy with JS than I could take a look at 
this:
 >
 > 
http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript-memory-leaks/

I'm not sure that the above article applies to WordPress. If you visit 
the website of the FF extension used to generate the leak reports (see 
http://dbaron.org/mozilla/leak-monitor/) you find this text:

"This Firefox extension detects one very specific type of leak in chrome 
JavaScript and in Web pages. (Not in JavaScript components, though.) It 
detects when JavaScript objects in the chrome or Web page are still held 
by native code after the window is closed."

Basically the extension looks to be for firefox and firefox plugin devs, 
not website authors.

That said, there are rumors of actual javascript memory leaks floating 
around the web. In particular, IE seems to have trouble reclaiming 
memory in certain instances. 
http://laurens.vd.oever.nl/weblog/items2005/closures/ looks like fun. If 
you find that mozilla/firefox is not reclaiming memory after you 
navigate away from a page, that is a bug in firefox and should be fixed. 
Of course there isn't a good way to test this, so probably not worth 
worrying about unless someone finds a specific instance that is causing 
browser trouble.

A quick digression:
It is not always necessary for a program to "clean up" after itself. For 
example, if I were writing a small utility in C and (for example) I 
malloc() the size of a particular file, read the file in and count the 
number of lines and print out the total count (I know there are more 
efficient ways to write this, plus I could just use the wc program) 
there is no reason for me to call free(). Sure, a leak manager will 
report that my program "leaks" memory but since the program isn't 
designed to sit around, as soon as it terminates the memory will be 
reclaimed by the OS (if the OS forgets to reclaim that memory, that is a 
bug in the OS, NOT the program). All operating systems that I know of do 
just that.
The same applies to web browsers. If I were designing a webapp where the 
user never navigates away from a page and all interaction is done 
through AJAX (like gmail?), I would be concerned about memory leaks. But 
so long as the JS implementation cleans up once I navigate away (and 
everything I have read indicates that at least firefox seems to do this 
properly in most cases) memory management/leaks shouldn't matter.

Of course, a browser restart will always fix everything.

Summary: I don't think any cycles need to be wasted looking at JS leaks. 
WP doesn't even use _that_ much JS in the grand scheme of things.

-Adam Batkin



More information about the wp-hackers mailing list