问候,
我一直在为一些使用 8 位微控制器的硬件开发网络界面。该网页使用 HTML、javascript、JSON 和 XHR (XMLHttpRequest) 进行通信。我想做的是创建一个页面,使用 setInterval 使用控制器中的新值每 250 毫秒更新一次,以便网页“实时”更新,让用户感觉更像是一个应用程序。
我已经让它大部分工作,但发现我测试过的两种浏览器(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 大小)进行了多次调用,因为这是存储来自微控制器的值的 JSON 对象;但是浏览器是否以某种方式在内存中存储/缓存每个页面?
我的评论中附有该问题的两个屏幕截图。第二个是我在 XMLHttpRequest 循环中设置断点的地方,看起来右侧的“closure”面板中有一个循环引用。有人看到这个问题吗?
我可以做些什么来更深入地挖掘并获取更多信息?
提前致谢!