问候,
我一直在为使用8位微控制器的某些硬件开发Web界面。该网页使用HTML,JavaScript,JSON和XHR(XMLHttpRequest)进行通信。我想要做的是创建一个页面,每隔250mS更新控制器的新值,使用setInterval,以便网页实时更新,使其更像是一个应用程序。使用XMLHttpRequest自动网页刷新内存泄漏
我已经完成了它的大部分工作,但发现代码中存在内存泄漏,包括我测试过的两种浏览器,IE和Chrome。
我在网上研究过,看起来像其他人有同样的问题,我试图执行不同的修复,但没有成功。
下面是一些代码快照,希望能够更好地解释一些事情,我修改了变量,使它们在没有看到完整应用程序的情况下变得更有意义。
// start the pageRefreshTimer to update values
var pageRefreshTimer = window.setInterval(updateValues, 250);
// Standard XHR opener
HTTP.getText = function(url, callback) {
var request = HTTP.newRequest(); // Searches array of standard XMLHttpRequest functions to try, code not shown...
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request.responseText) // responseText becomes JSONText below
}
}
request.open("GET", url);
request.send(null);
}
// Function that is constantly refreshed by HTML page to simulate real-time application
updateValues = function(parameter, value) {
newURL = newURL + "?" + parameter; // newURL is defined elsewhere in the code...
// Send the url and create the JSONObject
HTTP.getText(newURL, function(JSONText) {
var JSONObject = eval('(' + JSONText + ')'); // Specific notation for JSON
// Load object values into Javascript variables
Controller.detectorPosition = JSONObject.detectorPosition;
Controller.offset = JSONObject.offset;
Controller.actuatorPosition = JSONObject.actuatorPosition;
});
delete JSONObject; // My attempt at manual garbage collection, didn't resolve the memory leak
}
供您参考,会从微控制器发送到浏览器会是这个样子的JSON文件...
{ "offset": "1500",
"detectorPosition": "1558",
"actuatorPosition": "120" }
这是否看起来像一个问题,在“倒闭潮”代码?
使用Chrome中的开发工具(Ctrl-Shift-J),我注意到有多个对ParameterValues.json文件(350B大小)的调用,因为这是存储来自微控制器;但浏览器以某种方式在内存中存储/缓存每个页面?
附加在我的评论是两个屏幕截图的问题。第二个是我在XMLHttpRequest循环中设置断点的地方,看起来右边的“closure”面板中有一个循环引用。任何人都看到这个问题?
我能做些什么来深入挖掘并获得更多信息?
在此先感谢!
Chrome快照:http://i55.tinypic.com/fuyzaw.png – Lemtronix 2010-10-05 14:02:01
这是Chrome的开发者工具的另一个屏幕快照。我在XMLHttpRequest循环中设置了一个断点,看起来右边的“闭包”面板中有一个循环引用。 http://i54.tinypic.com/qyzprt.png – Lemtronix 2010-10-05 18:29:11