从Web提取JSON时发生内存泄漏 [英] Memory Leak When Pulling JSON from WEB
问题描述
我已经花了几天的时间,从我能想到的各个角度去实现它.我正在研究一个简单的Windows 7小工具.该脚本将从远程Web服务器提取JSON数据并将其放在页面上.我将jQuery 1.6.2用于$ .getJSON.脚本在每个循环中消耗更多的内存.
I've spent days on this and hit it from every angle I can think of. I'm working on a simple windows 7 gadget. This script will pull JSON data from a remote web server and put it on the page. I'm using jQuery 1.6.2 for the $.getJSON. Script consumes more memory each loop.
var count = 1;
$(document).ready(function () {
updateView();
});
function updateView(){
$("#junk").html(count);
count++;
$.getJSON( URL + "&callback=?", populateView);
setTimeout( updateView, 1000 );
}
function populateView(status) {
$("#debug").html(status.queue.mbleft + " MB Remaining<br>" + status.queue.mb + " MB Total");
}
任何帮助将不胜感激....谢谢!
Any help would be greatly appreciated....Thank you!
编辑:添加JSON数据示例
Add JSON data sample
?({"queue":{"active_lang":"en","paused":true,"session":"39ad74939e89e6408f98998adfbae1e2","restart_req":false,"power_options":true,"slots":[{"status":"Queued","index":0,"eta":"unknown","missing":0,"avg_age":"2d","script":"None","msgid":"","verbosity":"","mb":"8949.88","sizeleft":"976 MB","filename":"TestFile#1","priority":"Normal","cat":"*","mbleft":"975.75","timeleft":"0:00:00","percentage":"89","nzo_id":"-n3c6z","unpackopts":"3","size":"8.7 GB"}],"speed":"0 ","helpuri":"","size":"8.7 GB","uptime":"2d","refresh_rate":"","limit":0,"isverbose":false,"start":0,"version":"0.6.5","new_rel_url":"","diskspacetotal2":"931.51","color_scheme":"gold","diskspacetotal1":"931.51","nt":true,"status":"Paused","last_warning":"","have_warnings":"0","cache_art":"0","sizeleft":"976 MB","finishaction":null,"paused_all":false,"cache_size":"0 B","finish":0,"new_release":"","pause_int":"0","mbleft":"975.75","diskspace1":"668.52","scripts":[],"categories":["*"],"darwin":false,"timeleft":"0:00:00","mb":"8949.88","noofslots":1,"nbDetails":false,"eta":"unknown","quota":"","loadavg":"","cache_max":"0","kbpersec":"0.00","speedlimit":"","webdir":"","queue_details":"0","diskspace2":"668.52"}})
精简代码至此,仍然泄漏.我认为这消除了作为贡献者遍历DOM.
EDIT 2: Stripped code down to this and it still leaks. I think that eliminates traversing the DOM as a contributor.
$(document).ready(function () {
setInterval(updateView, 1000);
});
function updateView(){
$.getJSON( URL + "&callback=?", populateView);
}
function populateView(status) {
}
不是jQuery.我删除了jQuery,并使用了直接的js.仍然泄漏.
EDIT 3: It's not jQuery. I removed jQuery and did it with straight js. Still leaks.
function init(){
setInterval(updateView, 1000);
}
function updateView(){
var xhr = new XMLHttpRequest();
xhr.open("GET", URL, false);
xhr.setRequestHeader( "If-Modified-Since", "0");
xhr.send('');
}
所以...如果不是jQuery,不仅是IE(Chrome也是如此).有没有搞错?!想法?
So...if it's not jQuery, not just in IE (Chrome too). What the heck?! Ideas?
谢谢!
推荐答案
我感觉updateView中的setTimeout函数正在引起这种现象.要对此进行测试,您可以将代码修改为:
I have the feeling that the setTimeout function within the updateView is causing this behaviour. To test this you can modify your code to:
$(document).ready(function () {
setInterval(updateView, 1000);
});
function updateView(){
$("#junk").html(count);
count++;
$.getJSON( URL + "&callback=?", populateView);
}
function populateView(status) {
$("#debug").html(status.queue.mbleft + " MB Remaining<br>" + status.queue.mb + " MB Total");
}
setInterval函数将每隔x毫秒执行一次传入的函数. 此处到文档.
The setInterval function will execute the passed in function over and over every x miliseconds. Here to the docs.
另一个松动的性能(尽管对这个问题可能并不重要)是您每秒遍历DOM来找到$('#debug')
元素.您可以将其存储并传递为:
EDIT 2:
Another performance loose (Although it might not be critical to the issue) is that you are traversing the DOM every second to find the $('#debug')
element. You could store that and pass it in as:
$(document).ready(function () {
var debug = $('#debug');
var junk = $('#junk') ;
setInterval(function(){updateView(debug, junk)}, 1000);
});
function updateView(debug, junk){
junk.html(count);
count++;
$.getJSON( URL + "&callback=?", function(status){populateView(status,debug)});
}
function populateView(status) {
debug.html(status.queue.mbleft + " MB Remaining<br>" + status.queue.mb + " MB Total");
}
我更改了上面的代码,因为我忘记了接受服务器的响应.假设queue
是返回的JSON的属性,则代码应如上所述.
Edit 3: I have changed the code above because I forgot to take in the response from the server. Assuming that queue
is a property of the returned JSON then the code should be as above.
这是一个非常有趣的问题.然后是另一种方法.那么,让我们假设仍然有一些客户端脚本正在阻塞内存.这可能是什么?据我了解,剩下的只有两件事是setInterval和$ .getJSON函数. $ .getJSON函数是一个简单的ajax请求包装器,它引发一个请求并等待服务器的响应. setInterval函数是一个比较特殊的函数,因为它将设置计时器,触发函数等.
Edit 4: This is a very interesting issue. Another approach then. Lets assume then that there is still some client side scripts that are clogging the memory. What could this be? As far as is I understand the only two things left are the setInterval and the $.getJSON function. The $.getJSON function is a simple ajax request wrapper which fires a request and waits for the response from the server. The setInterval function is a bit more peculiar one because it will set up timers, fire functions, etc.
我认为,如果您设法在服务器上模仿它,甚至只是每5秒刷新一次浏览器中的网页,您就可以看到是处理请求的是客户端还是服务器.
I think if you manage to mimic this on your server or even just refresh this webpage in your browser every second/5 secs you you will be able to see whether it is the client or the server that processes your request.
这篇关于从Web提取JSON时发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!