Search code examples
ajaxcomet

AJAX memory leak/bloat


I'm doing a real-time chat application with a web interface, and I am getting a constantly growing memory footprint in FF5 (Linux binary). Curiously, Chromium doesn't exhibit the bloat. What I'm doing is the following:

1) A function kick-starts the cycle:

function init_chat ()
        {
            doAjax ("my-url", handler_name);
        }

2) The doAjax function:

function doAjax(address, ajax_handler)
        {   
            var xmlhttp;

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            } 

            xmlhttp.onreadystatechange = function() {ajax_handler(xmlhttp);};

            xmlhttp.open("GET", address, true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send();
        }

3) On the server, the request-thread waits at most 3 seconds for new info, and returns a JSON response

4) The handler function processes the response and calls doAjax again, with itself as the handler function.

From what I understand, this isn't true recursion, as the ajax call should spawn a new thread, and the handler function shouldn't theoretically hold a jump pointer back to the doAjax function. Maybe I'm creating a closure and it's not being collected properly? If so, how can I avoid it?

Thanks in advance, Vic.


Solution

  • I have seen similar things with FF - usually the extreme memory bloat comes from plugins like firebug; however, I always recommend manually nullifying objects in JS to force memory clearing. Memory management with JS is generally poor, it is best practice to clean up after yourself..manually :(