# Finding memory leaks?

**URL:** <https://www.ruby-forum.com/t/finding-memory-leaks/51889>\
**Category:** Rails\
**Created:** [January 8, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889 "2006-01-08T20:01:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bogdan\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@Bogdan\_I](https://www.ruby-forum.com/u/Bogdan_I)\
**Post date:** [January 8, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889/1 "2006-01-08T20:01:55Z")

</div>

Where are the memory leaks and what is the way to fix them?  
I’m working on a game that was fairly stable in terms the memory  
consumption  
and it was staying at around 200MB. Recently it has gone crazy, and  
unless I  
restart it, it goes straight up to 350-400+MB after 30 minutes-1hour.  
I am using Apache 1.3+fcgi in production mode.  
Also the dispatch.fcgi processes take a really high toll (3-5%) on the  
CPU.  
( 2.4GZ Xeon with 2 procs)  
Since it’s hosted (still) on a shared machine I have to constantly keep  
an  
eye on it.  
Any ideas, suggestions?

Thanks,  
Bogdan

---

<div class="post-metadata">

**Author:** ![Bogdan\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@Bogdan\_I](https://www.ruby-forum.com/u/Bogdan_I)\
**Post date:** [January 8, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889/2 "2006-01-08T22:45:02Z")

</div>

On 1/8/06, Bogdan I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Where are the memory leaks and what is the way to fix them?  
> I’m working on a game that was fairly stable in terms the memory consumption  
> and it was staying at around 200MB. Recently it has gone crazy, and unless I  
> restart it, it goes straight up to 350-400+MB after 30 minutes-1hour.  
> I am using Apache 1.3+fcgi in production mode.  
> Also the dispatch.fcgi processes take a really high toll (3-5%) on the CPU.  
> ( 2.4GZ Xeon with 2 procs)  
> Since it’s hosted (still) on a shared machine I have to constantly keep an  
> eye on it.  
> Any ideas, suggestions?

It used to be the case that Rails would leak memory when running in  
development mode. Not sure if it is anymore.

---

<div class="post-metadata">

**Author:** ![Bogdan\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@Bogdan\_I](https://www.ruby-forum.com/u/Bogdan_I)\
**Post date:** [January 8, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889/3 "2006-01-08T23:03:11Z")

</div>

My application is running in production mode, so it can’t be that

---

<div class="post-metadata">

**Author:** ![Bogdan\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@Bogdan\_I](https://www.ruby-forum.com/u/Bogdan_I)\
**Post date:** [January 9, 2006, 3:40am UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889/4 "2006-01-09T03:40:41Z")

</div>

Bogdan I. \<bogdan.ionescu@…\> writes:

> Where are the memory leaks and what is the way to fix them?I’m working on a  
> game that was fairly stable in terms the memory consumption and it was  
> staying  
> at around 200MB. Recently it has gone crazy, and unless I restart it, it  
> goes  
> straight up to 350-400+MB after 30 minutes-1hour.  
> I am using Apache 1.3+fcgi in production mode.Also the dispatch.fcgi processes  
> take a really high toll (3-5%) on the CPU. ( 2.4GZ Xeon with 2  
> procs)Since it’s  
> hosted (still) on a shared machine I have to constantly keep an eye on  
> it.  
> Any ideas, suggestions?Thanks,Bogdan

Bogdan,

Two things to look at:

1. What garbage collection settings do you have in public/dispatch.fcgi?  
If you  
use this syntax:

RailsFCGIHandler.process! nil, 50

Then garbage collection will NOT run until after 50 requests. This one  
bit us a  
while ago. Use the default instead if your dispatch.fcgis are growing:

RailsFCGIHandler.process!

1. Here’s a good article on finding leaky objects in Ruby. The tools  
aren’t  
terribly sophisticated but they work just fine if you’re willing to  
spend a  
little bit of time chasing your leak.

[http://theexciter.com/articles/finding-leaking-ruby-objects](http://theexciter.com/articles/finding-leaking-ruby-objects)

I got the best results out of using this code to analyze controller  
methods  
invoked through functional tests. Don’t be afraid to use vendor/plugins  
to  
override (and inject measurement code) into base Rails classes if the  
leak  
doesn’t appear to be in your code.

Best of luck,

Tyler

---

<div class="post-metadata">

**Author:** ![Bogdan\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@Bogdan\_I](https://www.ruby-forum.com/u/Bogdan_I)\
**Post date:** [January 9, 2006, 5:08am UTC](https://www.ruby-forum.com/t/finding-memory-leaks/51889/5 "2006-01-09T05:08:14Z")

</div>

Also, if you are using RMagick, then you will need to perform manual  
garbage  
collection to get all the used memory back.

I have a method that looks like this that I call after I do a bunch of  
operations:

def run\_gc  
fDisabled = GC.enable  
GC.start  
GC.disable if fDisabled  
end
