CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Potential Memory Leak when with Ajax (http://www.codingforums.com/showthread.php?t=272125)

smcf 09-03-2012 09:27 AM

Potential Memory Leak when with Ajax
 
Hi all.
I'm using JavaScript to call a server side service provided by an IBMBPM Tool (Lombardi teamworks), to retrieve an XML object.

I've stripped it down to the bare essentials below.
When I click on the button, it runs the call 5000 times.
Any browser grabs a whole load of memory and never lets go of it. What Am I doing wrong here?

The code is below.

Code:

<script>

    function makeRequest(url, params, callBack) {
        var http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {
                if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                callBack(http_request.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }};
        http_request.open('GET', url, true);
        http_request.send(params);
    }

tw.coach.callServiceRegus = function(service, params, callBack, snapshot) {
 if(snapshot == "" || snapshot == null) {
    // coachSnapshotContext is injected via the CoachDesigner.xsl
    snapshot = coachSnapshotContext;
 }
 var url = "/portal/jsp/callService.do?serviceName=" + service + "&snapshotId=" + snapshot;
 makeRequest(url, params, callBack);
};


function getTickets(){
    var data;
    var inputVar = '<input><variable name="inputVar">dion.jones</variable>'
    inputVar += '<variable name="sortOrder">t.Priority, t.CreateDate</variable>'
    inputVar += '<variable name="displayType">UserLandingPage</variable>'
    inputVar += '</input>';
    tw.coach.callServiceRegus('SORGetUserTicketsXML',
      inputVar,
      function(data){
      }
    );
}


function explode(){
for (var i = 0 ; i < 5001 ; i++) {
getTickets();
}
alert("done");
}
</script>




This button is in the HTML on the page.

<!-- Refrech Button -->

<input type="button" onclick="explode();" value="clickerty-Click!" />


smcf 09-03-2012 09:32 AM

I apologise for the Thread title also - my brain is obviously malfunctioning this morning and I can't find a way to edit it.

DaveyErwin 09-03-2012 05:00 PM

Making 5000 XMLHttprequest objects
will never be a good idea.You should
make 4 at the start of your code and
reuse them, it is not useful to have
more that 4 making a requst of the
same URL at the same time as most
browser are limited to 4 or less connections
to the same URL at the same time.

smcf 09-03-2012 09:25 PM

i see.
I was just using the 5000 calls to test and magnify the memory leak issue.

Are you saying I should just call 4 and then loop using these?


All times are GMT +1. The time now is 10:55 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.