Ruby / ruby ​​memory leak detection on rails

I wrote a small web application using ruby ​​on rails, its main purpose is to download, save and display the results from xml files (files can be up to several MB). After working for about 2 months, I noticed that the mongrel process uses about 4 GB of memory. I did some research on debugging ruby ​​memory leaks and couldn't find much. Therefore, I have two questions.

  • Are there any good tools you can use to find memory leaks in Ruby / rails?
  • What types of coding cause memory leak in ruby?
+45
ruby coding-style memory ruby-on-rails memory-leaks
Oct 02 '08 at 8:15
source share
7 answers

Some tips for finding memory leaks in Rails:

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

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

As for specific coding patterns, from experience you should observe everything related to input-output files, image processing, work with massive strings, and the like.

I would check if you use the most suitable XML library - it is known that ReXML is slow and is considered a leak (I have no evidence of this!). Also check if you can memorize expensive operations.

+38
Oct 02 '08 at 11:53
source share

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

#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 the script / console and try to run the output first to make sure it works in your field.

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

Then just look at the top, when the request forces you to use memory, go to the log. This, of course, will only help if you have a memory leak that occurs during large jumps, and not in minimal increments.

+17
Oct 02 '08 at 19:46
source share

A memory leak is a problem in the current ruby ​​implementation. A good place to start about this is http://whytheluckystiff.net/articles/theFullyUpturnedBin.html Why the thelulucystiff website no longer exists, but you can find the original article here: https://viewsourcecode.org/why/hacking/ theFullyUpturnedBin.html

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

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

+6
02 Oct '08 at 10:35
source share

You should take a look at ruby-prof .

+5
Aug 31
source share

Switch to jruby and use the Eclipse Memory Analyzer . There is currently no comparable tool for Ruby.

+2
Oct 10 '08 at 15:35
source share

Now you can run the following to get the memory in a format that R can read. I assume your log line is as follows:

 1234567890 RAM USAGE: 27456K 

Run this (or change to set):

 $ grep 'RAM USAGE' fubar.log | awk '{print s " " $1 " " $4; s++}' | sed 's/K//g' > mem.log 

Then you can run this:

 #!/bin/sh rm -f mem.png R --vanilla --no-save --slave <<RSCRIPT lst <- read.table("mem.log") attach(lst) m = memory / 1024.0 summary(m) png(filename="mem.png", width=1024) plot(date, m, type='l', main="Memory usage", xlab="time", ylab="memory") RSCRIPT 

and get a good graph.

+2
Mar 04 '10 at 8:32
source share

These gems worked for me:

Memorylogic

Adds proxies to id and memory usage in your rail logs, great for tracking memory leaks

Oink

Log parser to identify actions that significantly increase VM heap size

0
Mar 17 '14 at 21:03
source share



All Articles