javascript - 树莓派 JavaScript 内存泄漏

标签 javascript memory-leaks coffeescript midori

这段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/

相关文章:

c++ - Valgrind 在程序出现段错误之前就以 "pure virtual method called"中止

memory-leaks - 这个 D 示例中是否存在内存泄漏?

javascript - Coffeescript 中的 promise 。尝试将多个参数传递给 `unexpected comma` 时出现 `then` 错误?

javascript - 阻止 Div 滚出 View

javascript - 使用 Javascript 简单添加类

javascript - 使用 jQuery 获取元素的所有属性

c++ - _CrtSetBreakAlloc 跟踪 COM 对象中的内存泄漏

javascript - 使用 Geocoder() 时,MarkerWithLabel 仅显示 google map api 中的最后一个值

ruby - 为什么这个 CoffeeScript 比这个 Ruby 脚本快?

javascript - 将字符串转换为数字或空的简单方法?