IE memory leak, how to resolve?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizwan6feb
    New Member
    • Jul 2007
    • 108

    IE memory leak, how to resolve?

    I am working on an ajax chat application, done most of the work but when i tested it on IE (both on IE6 and IE7), found that there is a memory leak. The application works fine on FF. How can i fix this?

    I have used ajax in the following way (this is a similar to how i am using ajax in my chat application); incrementer.php file on line # 34 simply prints an incremented value from the session

    Code:
    <html>
    <head>
    <script language="javascript">
    	function getAjaxObject(){
    		try{
    			ajaxRequest = new XMLHttpRequest();
    		} catch (e){
    			// Internet Explorer Browsers
    			try{
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try{
    					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e){
    					// Something went wrong
    					alert("Your browser doesn't support ajax!");
    					return false;
    				}
    			}
    		}
    		return ajaxRequest;	
    	}
    	function startCounting(){
    		var a=getAjaxObject();
    		a.onreadystatechange = function(){
    			if(a.readyState==4){
    				var text=a.responseText;
    				var e=document.getElementById('val');
    				if(e){
    					e.innerHTML=text;
    				}
    			}
    		}
    		a.open("GET", "incrementer.php", true);
    		a.send(null);
    		setTimeout(startCounting,1);
    	}
    </script>
    </head>
    
    <body>
    	<input type="button" value="Send Object" onclick="startCounting()" />
    	<div id="val">	</div>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    1 millisecond is a bit too small, isn't it?

    Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link.

    Comment

    • rizwan6feb
      New Member
      • Jul 2007
      • 108

      #3
      Originally posted by acoder
      1 millisecond is a bit too small, isn't it?

      Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link.
      Thanks for your reply. Setting the interval to 1 millisecond i can see quick increase in the use of memory.

      I am unable to trace circular reference in my code. If there is any, please point to that & and how can i resolve this

      Comment

      • rizwan6feb
        New Member
        • Jul 2007
        • 108

        #4
        I have found a solution which leads to another problem. The solution is given below, i have got rid of inner function (see line # 38 ). Now there is an error at line# 28 as i have no access to variable 'a' within updateText The problem is how can i access variable 'a' in the updateText function.
        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script language="javascript">
        	var x=0;
        	function getAjaxObject(){
        		try{
        			ajaxRequest = new XMLHttpRequest();
        		} catch (e){
        			// Internet Explorer Browsers
        			try{
        				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        			} catch (e) {
        				try{
        					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        				} catch (e){
        					// Something went wrong
        					alert("Your browser doesn't support ajax!");
        					return false;
        				}
        			}
        		}
        		return ajaxRequest;	
        	}
        	function updateText(){
        		if(a.readyState==4){
        			var text=a.responseText;
        			var e=document.getElementById('val');
        			if(e){
        				e.innerHTML=text;
        			}
        		}
        	}
        	function startCounting(){
        		var a=getAjaxObject();
        		a.onreadystatechange = updateText;
        		a.open("GET", "incrementer.php", true);
        		a.send(null);
        		setTimeout(startCounting,1);
        	}
        </script>
        </head>
        
        <body>
        	<input type="button" value="Send Object" onclick="startCounting()" />
        	<div id="val">	</div>
        </body>
        </html>

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          One solution is to make 'a' global. If you don't want to do that, you can use a closure.

          Comment

          • rizwan6feb
            New Member
            • Jul 2007
            • 108

            #6
            Originally posted by acoder
            One solution is to make 'a' global. If you don't want to do that, you can use a closure.
            I can't use global variable and i have no idea how to use a closure. Please give me sample code

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Basically creating a function which returns a function, e.g.
              [code=javascript]function updateText2(a) {
              return function() {
              updateText(a);
              }
              }[/code] and calling that instead. See this link for an explanation of closures.

              Comment

              • rizwan6feb
                New Member
                • Jul 2007
                • 108

                #8
                I have used closure, but the problem persists. Please see the code below what i am doing wrong
                Code:
                <html>
                <head>
                <script language="javascript">
                	var x=0;
                	function getAjaxObject(){
                		try{
                			ajaxRequest = new XMLHttpRequest();
                		} catch (e){
                			// Internet Explorer Browsers
                			try{
                				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                			} catch (e) {
                				try{
                					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                				} catch (e){
                					// Something went wrong
                					alert("Your browser doesn't support ajax!");
                					return false;
                				}
                			}
                		}
                		return ajaxRequest;	
                	}
                	function updateText(a){
                		return function(){
                			if(a.readyState==4){
                				var text=a.responseText;
                				var e=document.getElementById('val');
                				if(e){
                					e.innerHTML=text;
                				}
                			}
                		};
                	}
                	function startCounting(){
                		var a=getAjaxObject();
                		var fun1=updateText(a);
                		a.onreadystatechange = fun1;
                		a.open("GET", "incrementer.php", true);
                		a.send(null);
                		setTimeout(startCounting,1);
                	}
                </script>
                </head>
                
                <body>
                	<input type="button" value="Start Counting" onclick="startCounting()" />
                	<div id="val">	</div>
                </body>
                </html>

                Comment

                • rnd me
                  Recognized Expert Contributor
                  • Jun 2007
                  • 427

                  #9
                  Code:
                  	function startCounting(){
                  		var a=getAjaxObject();
                  		a.onreadystatechange = function(){
                  			if(a.readyState==4){
                  				var text=a.responseText;
                  				var e=document.getElementById('val');
                  				if(e){
                  					e.innerHTML=text;
                  				}
                  				[U][B]a = null;[/B][/U]
                  			}
                  		}
                  		a.open("GET", "incrementer.php", true);
                  		a.send(null);
                  		setTimeout(startCounting,1);
                  	}

                  Comment

                  • rizwan6feb
                    New Member
                    • Jul 2007
                    • 108

                    #10
                    Yes, that works. Great job man, thank you very much

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      So simple that I didn't think of. Thanks rnd me ;)

                      Comment

                      Working...