morellon / rrd-ffi

A ruby wrapper for librrd (rrdtool) using ffi.
MIT License
71 stars 20 forks source link

Memory leak #1

Closed renchap closed 14 years ago

renchap commented 14 years ago

I use RRD-FFI in a Rails 3 application to fetch data from RRD files, and the RAM used by the thin process is growing over time (my application fetch data from a RRD file every 10 seconds). My code works like this :

rrd = RRD::Base.new(path)
results = rrd.fetch(:average, :start => start_date, :end => end_date)
info = rrd.info
# ... some processing
rrd = nil

Adding rrd = nil at the end helps a bit, but there is still some memory leaking. I think there can be the same problem as here : http://stackoverflow.com/questions/181406/ruby-memory-management/181433#181433. Maybe a rrd.destroy! method can be added, to force the release of all librrd + rrd-ffi memory ?

morellon commented 14 years ago

I thought ffi would take care of memory management for me. I will take a look more carefully on how ffi does it. I may need to manualy free some pointers (through ffi) used on the bindings or even using librrd memory management (rrd_freemem and rrd_info_free C functions) it self.

Thanks for the feedback. I will also make a deamon running the same methods you are using and try the solutions above. I think a destroy! should be the last resource, as the gem should work transparently to the end user. I will get in touch as soon as possible.

Thanks!

renchap commented 14 years ago

If this is useful : ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0] The webserver used is Thin. Webrick (using rails server from rails 3) crashes when i call rrd-ffi, dont know how to troubleshoot this)

morellon commented 14 years ago

Can you test again from source? I think i've fixed it.

renchap commented 14 years ago

I dont think its fixed. Here are the top results :

PID    COMMAND      %CPU TIME     #TH  #WQ  #POR #MRE RPRV RSHRD  RSIZ VPRV VSIZE  PGRP  PPID  STATE    UID
94807  ruby         5.7  00:06.99 2    0    36   213  71M  2456K  75M  84M  2464M  94807 94186 sleeping 501

PID    COMMAND      %CPU TIME     #TH  #WQ  #POR #MRE RPRVT RSHRD  RSIZE VPRVT VSIZE  PGRP  PPID  STATE    UID
94807  ruby         8.5  00:39.55 2/1  0    36   291  197M+ 2456K  201M+ 208M  2588M  94807 94186 running  501

There was a little more than 30s between two, with 1 call to my controller each second.

I bumbed the gemspec to 0.2.2, built and installed the gem, and checked bundler if all was right : $ bundle show Gems included by the bundle: ...

If you want to try, my code is here : http://github.com/renchap/graphs-on-rails You will need a simple collectd install (with memory or cpu plugins) to have some rrd files. You can accelerate the polling by changing http://github.com/renchap/graphs-on-rails/blob/master/app/views/metrics/show.html.haml#L36

morellon commented 14 years ago

renchap,

which version of rrdtool are you using? if it is 1.3.x try upgrading to 1.4.2

i tested with the code in examples/all.rb and i couldn't find a memory leak for both fetch and info on 1.4.2 using darwin10.2.0 and ruby 1.8.7/1.9.2.preview1

morellon commented 14 years ago

I will close it, please reopen if the issues reappears