ruby/ruby on rails memory leak detection ruby/ruby on rails memory leak detection ruby ruby

ruby/ruby on rails memory leak detection


Some tips to find memory leaks in Rails:

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.


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

#Put this in applictation_controller.rbbefore_filter :log_ram # or use after_filterdef log_ram  logger.warn 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].stripend

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.


Memory leak is a problem in the current ruby implementation a good place to start about this is http://whytheluckystiff.net/articles/theFullyUpturnedBin.html Whytheluckystiff website doesn't exist anymore but you can find the original article here: https://viewsourcecode.org/why/hacking/theFullyUpturnedBin.html

for a more specific answer on problems with long running ruby processes see https://just.do/2007/07/18/heap-fragmentation-in-a-long-running-ruby-process/

maybe you could give passenger (mod_rails) a try https://web.archive.org/web/20130901072209/http://nubyonrails.com/articles/ask-your-doctor-about-mod_rails