Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2012, 09:27 AM   PM User | #1
smcf
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smcf is an unknown quantity at this point
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 is offline   Reply With Quote
Old 09-03-2012, 09:32 AM   PM User | #2
smcf
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smcf is an unknown quantity at this point
I apologise for the Thread title also - my brain is obviously malfunctioning this morning and I can't find a way to edit it.
smcf is offline   Reply With Quote
Old 09-03-2012, 05:00 PM   PM User | #3
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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.
DaveyErwin is offline   Reply With Quote
Old 09-03-2012, 09:25 PM   PM User | #4
smcf
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smcf is an unknown quantity at this point
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?
smcf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 PM.


Advertisement
Log in to turn off these ads.