# Memory leak in Rails apps

**URL:** <https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327>\
**Category:** Ruby\
**Created:** [May 14, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327 "2010-05-14T17:16:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [May 14, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327/1 "2010-05-14T17:16:38Z")

</div>

We have developed rails reports and deployed in server, report have some  
hierarchy levels…

Initially we have less volume of data in database table and report is  
running with fine performance…

after few months,reports get very slower, we thought that this could be  
because of data increased in database table ( nearly 11 lakhs of records  
in table)…

Then i created index and report has shown better performance than  
old…

after 1 year, again reports are showing poor performance…  
So we have decided to monitor memory related issues and used ‘top’ and  
‘free’ commands to monitor memory…

as per our analysis,

1. at start, used memory was 100MB and free memory was 500MB.

2. When i continuously used complex report(i visit some hierarchy level  
or sub links) ,at first level shows good performance…then i move  
forward to next and next level reports the performance get very poor.

3. The free memory was reduced to 150MB…used memory was increased to  
400MB…

4. the free memory is not released even after the report shows result…

So we suggest to release used memory by forcing garbage collection  
manually…

Kindly tell me how to call garbage collection manually or any other  
ideas are also welcome…

Thanks…

---

<div class="post-metadata">

**Author:** ![jmanivannancse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@jmanivannancse](https://www.ruby-forum.com/u/jmanivannancse)\
**Post date:** [May 14, 2010, 7:22pm UTC](https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327/2 "2010-05-14T19:22:53Z")

</div>

On Fri, May 14, 2010 at 9:19 AM, Manivannanj J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kindly tell me how to call garbage collection manually or any other  
> ideas are also welcome…
> 
> Thanks…

You may start garbage collection manually in Ruby by calling GC.start,  
however on a production server it may be a good idea to restart your  
server every so often (there are things that the garbage collector  
does not collect, like symbols for instance). Deployment products  
like Passenger have methods for automatically restarting a server  
process after a certain amount of time or number of requests, you may  
want to look into that.

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![jmanivannancse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@jmanivannancse](https://www.ruby-forum.com/u/jmanivannancse)\
**Post date:** [May 15, 2010, 12:03am UTC](https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327/3 "2010-05-15T00:03:50Z")

</div>

> 1. When i continuously used complex report(i visit some hierarchy level  
> or sub links) ,at first level shows good performance…then i move  
> forward to next and next level reports the performance get very poor.
> 
> 2. The free memory was reduced to 150MB…used memory was increased to  
> 400MB…
> 
> 3. the free memory is not released even after the report shows result…
> 
> So we suggest to release used memory by forcing garbage collection  
> manually…

I have added some GC suggestions to the bottom of

[http://wiki.github.com/rdp/ruby\_tutorials\_core/gc](http://wiki.github.com/rdp/ruby_tutorials_core/gc)

That may be of interest to you.  
Your problem is actually a common one.  
Cheers!  
-rp

---

<div class="post-metadata">

**Author:** ![jmanivannancse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@jmanivannancse](https://www.ruby-forum.com/u/jmanivannancse)\
**Post date:** [May 15, 2010, 12:25am UTC](https://www.ruby-forum.com/t/memory-leak-in-rails-apps/197327/4 "2010-05-15T00:25:49Z")

</div>

On Fri, May 14, 2010 at 10:19 AM, Manivannanj J. \<  
removed\_email\_address@domain.invalid\> wrote:

> Then i created index and report has shown better performance than  
> 2) When i continuously used complex report(i visit some hierarchy level
> 
> Kindly tell me how to call garbage collection manually or any other  
> ideas are also welcome…
> 
> ## Thanks…
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

One of the talks at Ruby Midwest is going to be on Memprof. Here’s it’s  
abstract:

* * *

Memprof: the ruby level memory profiler

Aman G. & Joe Damato â€“ Ever wondered why your ruby script uses so  
much  
RAM? Tired of having god restart your rails app every few hours?

This talk will introduce memprof, a ruby level memory profiler. We’ll  
demo  
memprof on a variety of real-world examples, including some popular open  
source projects. Along the way, we’ll learn how the ruby VM manages  
memory,  
why garbage collection has such a large impact on ruby’s performance and  
some common ways to reduce the memory impact of your ruby applications.

* * *

Even if you can’t attend the conference, you might check out Memprof  
anyway,  
it sounds as though it might help you identify the cause of your issues  
so  
that you can fix the source of the problem. Note: I haven’t personally  
played with it (yet).
