开发者

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(40开发者_开发百科00);                        
    }            
    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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜