Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby/ruby on rails memory leak detection

I wrote a small web app using ruby on rails, its main purpose is to upload, store, and display results from xml(files can be up to several MB) files. After running for about 2 months I noticed that the mongrel process was using about 4GB of memory. I did some research on debugging ruby memory leaks and could not find much. So I have two questions.

  • Are there any good tools that can be used to find memory leaks in Ruby/rails?
  • What type of coding patterns cause memory leaks in ruby?
like image 489
Josh Moore Avatar asked Oct 02 '08 08:10

Josh Moore


2 Answers

Some tips to find memory leaks in Rails:

  • use the Bleak House plugin
  • implement Scout monitoring specifically the memory usage profiler
  • try another simple memory usage logger

The first is a graphical exploration of memory usage by objects in the ObjectSpace.

The last two will help you identify specific usage patterns that are inflating memory usage, and you can work from there.

As for specific coding-patterns, from experience you have to watch anything that's dealing with file io, image processing, working with massive strings and the like.

I would check whether you are using the most appropriate XML library - ReXML is known to be slow and believed to be leaky (I have no proof of that!). Also check whether you can memoize expensive operations.

like image 98
Dave Nolan Avatar answered Sep 19 '22 05:09

Dave Nolan


A super simple method to log memory usage after or before each request (only for Linux).

#Put this in applictation_controller.rb before_filter :log_ram # or use after_filter def log_ram   logger.warn 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip end 

You might want to load up script/console and try the statement out first to make sure it works on your box.

puts 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip 

Then just monitor top, when a request makes your memory usage jump, go check the logs. This, of course, will only help if you have a memory leak that occurs in large jumps, not tiny increments.

like image 20
Daniel Beardsley Avatar answered Sep 19 '22 05:09

Daniel Beardsley