实现有价值的IT服务
全部博文(709)
分类: Java
2006-12-20 11:00:21
最近做一个实时监控系统,用到了ajax技术,采用了所谓比较成熟的开源框架dwr,但是页面用setTimeout(method,1000)每秒刷新一次,服务器端倒没问题,客户端ie占用内存猛增,关键是一直持续上涨,产生内存泄漏(memeory leak)。
我研究了一下DWR的engin.js,结果看到页面每次调用都产生一个新的XMLHttpRequest或者DWREngine._newActiveXObject(DWREngine._XMLHTTP)对象,所以将其改为全局变量,只产生一个即可。
同一个页面多次产生该对象内存将不会被系统回收,导致一直增长的情况发生,改动之后就没问题了。
改前的
/** @private Actually send the block of data in the batch object. */
DWREngine._sendData = function(batch) {
..................
// Get setup for XMLHttpRequest if possible
if (batch.method == DWREngine.XMLHttpRequest) {
if (window.XMLHttpRequest) {
batch.req =new XMLHttpRequest();
}
// IE5 for the mac claims to support window.ActiveXObject, but throws an error when it's used
else if (window.ActiveXObject && !(navigator.userAgent.indexOf("Mac") >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)) {
batch.req = DWREngine._newActiveXObject(DWREngine._XMLHTTP);
}
}
........
};
改后的:
var myActiveXObject=null;
var myXMLHttpRequest=null;
/** @private Actually send the block of data in the batch object. */
DWREngine._sendData = function(batch) {
..................
// Get setup for XMLHttpRequest if possible
if (batch.method == DWREngine.XMLHttpRequest) {
if (window.XMLHttpRequest) {
if(myXMLHttpRequest==null){
//alert("myActiveXObject IS NULL ");
myXMLHttpRequest= new XMLHttpRequest();
}
batch.req =myXMLHttpRequest;
}
// IE5 for the mac claims to support window.ActiveXObject, but throws an error when it's used
else if (window.ActiveXObject && !(navigator.userAgent.indexOf("Mac") >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)) {
if(myActiveXObject==null){
myActiveXObject=DWREngine._newActiveXObject(DWREngine._XMLHTTP);
}
batch.req = myActiveXObject;
}
}
........
};