Memory leaks and JQuery and htmlunit and...

(Just a quick note about JQuery and rdfquery and memory leaks and htmlunit and such like for lost souls googling desperately for clues.)

I was having issues with some Webdriver tests, running under the htmlunit driver: they worked locally, but ran into Java "out of memory: heap space exhaustion" issues when running as part of a Hudson build on our Linux build server. The build also worked on another virtual machine with the Internet Explorer driver, so it seemed to be something specific to the htmlunit driver (maybe). After upgrading Java, upgrading Hudson (the continuous integration server), and tweaking the Java heap size, and still no joy, I found a few mentions of memory leaks in relation to htmlunit and JQuery (the Javascript library we're using), which I decided were worth investigating.

One comment in particular seemed plausible: that the Javascript on the pages under test was causing memory leaks in the Javascript engine in htmlunit (Rhino, I think?). It seemed possible that how we were using JQuery was causing Javascript to leak memory, but only in the context of the htmlunit driver. I trawled around the various bug trackers for htmlunit and webdriver, and got a few ideas (though couldn't pin the issue down enough to raise a bug myself).

One suggested fix (in the general context of JQuery, rather than specifically with regard to htmlunit) was to ensure that any event handlers bound to DOM elements via JQuery should explicitly be removed when the page unloads. This was easy to implement: I just added a final event binding to each page which looked like this (inside a <script> tag, obviously):

$(document).unload(function() {$('*').unbind(); });

I was also using the short-hand JQuery event binding syntax in my code, i.e. things like this:

$('#save').click(function() {
   // ...
});

I changed these to the long-hand form (as this issue only started appearing when I started using the short-hand version):

$('#save').bind('click', function() {
   // ...
});

One last thing I did was ensure that I called the quit() method on any Webdriver driver instances after I'd finished with them; and closeAllWindows() on any htmlunit WebClient instances when finished with them.

Unfortunately, I didn't do this very scientifically, and made all these changes at once. But the end result was that the build started running again. So if you're having out of memory errors with Webdriver/htmlunit/Hudson/JQuery/rdfquery, you at least have somewhere to start from :)

Comments

Hey!`

Would you plz give some post on PHP? and wordpress?

What do you want to know?

What do you want to know?

Seen the same in Prototypejs in Aspire

Elliot,

I've seen the same kind of issue in Aspire a while ago - using Prototype instead of JQuery though. In Aspire, where we have our rich client drag/drop interface I have to be careful to detach any events from DOM elements before those elements are removed from the DOM. I stumbled on this in much the same manner you did by the sound of it, but it all sounds perfectly obvious when you sit back and think about it. I just wish it had seemed more obvious at the time!

Andrew

Thanks for that Andrew. That

Thanks for that Andrew. That actually makes me think I should go back and ensure that any event handlers are removed if/when we remove DOM elements - we're not currently doing this, only removing event handlers when the page is unloaded.