简体   繁体   中英

memory leak in ajax - setInterval

I have an ajax code which causes memory leak (especially in IE).

function setStatus() {             
    var formInput=$(this).serialize(); 
    $.getJSON('CheckStatus.action', formInput, function(data) {                                   
            if(data == false) {    
                function getEventsPeriodicaly() {
                    getEvents();
                };

                var timer = setInterval(function () {getEventsPeriodicaly();}, 5000);
            }
        }
    );
} 

function getEvents() {
    var formInput=$(this).serialize(); 
    $.getJSON('StartEP.action', formInput,function(data) {                 
       var txt = $("#txtEventsArea");             
       if(data != null && data.toString().length!=0) {                                                    
         txt.val(data.join('\n') + '\n' +  txt.val()); 
         data=null;
       } 
    }
)}

StartEP

public String startEP() throws Exception {
    logger.info("[EP] In startEP");
    try { 
        synchronized(status) {
            if(!getStatus()) {     
                EventProcessor amiep = EventProcessor.getInstance();                             
                amiep.addObserver(this);
                new Thread(amiep).start();                                           
                setStatus(true);                
            }            
        }

    } catch (Exception ex) {     
         logger.error("Unable to start EP", ex);                 
         return ERROR;                 
    }
    logger.info("[EP] In startEP, before loop");
    while(!gotNewData) {
        Thread.sleep(4000);                        
    }            
    gotNewData = false;
    logger.info("[EP] Out startEP");
    return SUCCESS;
}

The StartEP action returns messages (about 5KB on each request). First I thought it concerned with setting text to textarea, but after some tests got that it is not the reason. Could it be setInterval method? Is there any considerations? thanks

I would say this looks pretty suspect:

while(!gotNewData) {
    Thread.sleep(4000);                        
}

Where is gotNewData set? If you call the web service once and set gotNewData to true and then call another web service and set gotNewData to false I don't think there is a guarantee that you're setting the variable within the same instance of the application. Therefore, if you are not, then every time you're hitting the web service you are starting a new thread and then continually putting it back to sleep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM