简体   繁体   中英

Raspberry pi javascript memory leak

This coffeescript code (rails server) causes midori to use more and more memory, so clearly contains a memory leak. This is causing my pi to crash after several hours. Can anyone please help me identify the source of the leak?

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

I have also tried following code, to no avail.. :

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))

edit: for completeness, the generated 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

Just a theory, but perhaps you are creating a new anonymous functions on every interval run. And it's not getting garbage collected.

I would try extracting the function outside of the interval loop, so it's defined only once:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM