这段coffeescript代码(rails服务器)导致midori使用越来越多的内存,因此显然包含内存泄漏。这导致我的 pi 在几个小时后崩溃。谁能帮我确定泄漏的来源吗?
jQuery ->
title = ""
dir = ""
window.setInterval ->
$.get('p_status', (response) ->
title = jQuery.parseJSON(response.title)
dir = jQuery.parseJSON(response.dir)
$("#current_title").html(title)
$("#main_c").attr("src", dir))
, 8000
我也尝试过以下代码,但无济于事..:
jQuery ->
title = ""
dir = ""
ref_current_title = $("#current_title")
ref_main_c = $("#main_c")
window.setInterval ->
$.get('p_status', (response) ->
title = jQuery.parseJSON(response.title)
dir = jQuery.parseJSON(response.dir)
ref_current_title.html(title)
ref_main_c.attr("src", dir))
编辑:为了完整性,生成的 javascript:
(function() {
jQuery(function() {
var title, dir, ref_current_title, ref_main_c ;
title = "";
dir = "";
ref_current_title = $("#current_title");
ref_main_c = $("#main_c");
return window.setInterval(function() {
return $.get('p_status', function(response) {
title = jQuery.parseJSON(response.title);
dir = jQuery.parseJSON(response.dir);
ref_current_title.html(title);
return ref_main_c.attr("src", dir);
}
});
}, 8000);
});
}).call(this);
最佳答案
Both the rails server and midori are on the same raspberry pi. It is the midori process that is accumulating memory
只是一个理论,但也许您正在每次间隔运行时创建一个新的匿名函数。而且它没有被垃圾收集。
我会尝试在间隔循环之外提取函数,因此它仅定义一次:
get_p_status =
() ->
$.get('p_status', (response) ->
title = jQuery.parseJSON(response.title)
dir = jQuery.parseJSON(response.dir)
$("#current_title").html(title)
$("#main_c").attr("src", dir)
)
jQuery ->
title = ""
dir = ""
window.setInterval get_p_status, 8000
关于javascript - 树莓派 JavaScript 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24232138/