简体   繁体   English

ajax 中的 memory 泄漏 - setInterval

[英]memory leak in ajax - setInterval

I have an ajax code which causes memory leak (especially in IE).我有一个 ajax 代码导致 memory 泄漏(尤其是在 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启动EP

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). StartEP 操作返回消息(每个请求大约 5KB)。 First I thought it concerned with setting text to textarea, but after some tests got that it is not the reason.首先我认为它与将文本设置为textarea有关,但经过一些测试后发现这不是原因。 Could it be setInterval method?可以是 setInterval 方法吗? Is there any considerations?有什么考虑吗? thanks谢谢

I would say this looks pretty suspect:我会说这看起来很可疑:

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

Where is gotNewData set? gotNewData 集在哪里? 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.如果您调用一次 web 服务并将 gotNewData 设置为 true,然后调用另一个 web 服务并将 gotNewData 设置为 false,我认为不能保证您在应用程序的同一实例中设置变量。 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.因此,如果您不是,那么每次您访问 web 服务时,您都在启动一个新线程,然后不断地使其重新进入睡眠状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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