1

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);
halcyon10
  • 53
  • 4
  • So the rails server is on raspberry pi? And you're polling the server every 8 sec with this client-side code? So the memory leak should be within rails code. What are you deploying on? How many rails worker processes? Would definitely not deploy on passenger and not using more than 1 worker. Preferably would use some multithreaded deployment server e.g puma and rubinius for real threading. On most app server there is also a max memory config which will restart the rails process after it hits the limit – dre-hh Jun 15 '14 at 22:50
  • Both the rails server and midori are on the same raspberry pi. It is the midori process that is accumulating memory. – halcyon10 Jun 16 '14 at 12:59

1 Answers1

0

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
dre-hh
  • 7,840
  • 2
  • 33
  • 44