# Understanding memory leaks

**URL:** <https://www.ruby-forum.com/t/understanding-memory-leaks/104214>\
**Category:** Rails\
**Created:** [May 15, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/understanding-memory-leaks/104214 "2007-05-15T14:34:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [May 15, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/understanding-memory-leaks/104214/1 "2007-05-15T14:34:55Z")

</div>

Hey list,

My app is leaking; It’s actually more of a gush than just a leak.

I tried using the script from  
[http://scottstuff.net/blog/articles/2006/08/17/memory-leak-profiling-with-railsbut](http://scottstuff.net/blog/articles/2006/08/17/memory-leak-profiling-with-railsbut)  
to be honest, I couldn’t discern any noticeable pattern from its  
output.  
From looking at top and clicking various pages on my site, I have a  
rough  
idea where the leak is coming from, but I think I need some help with  
understanding what exactly is capable of causing them.

I have a class with a bunch of class methods and a class variable (I  
prefer  
this approach over singleton instances). Through the cycle of a single  
request, this class stores various objects in a class variable. At the  
_start_ of the request, before the class is used, a reset method is  
called  
to assign nil to the class variable. At the end of the request, the  
class is  
left as-is, with the class variable still populated with objects. Am I  
correct in my assumption that it doesn’t matter if I call the reset  
method  
at the start or end of the request cycle? In production mode, the state  
of  
the class should surely be unchanged between requests?

Other than this situation, I fail to see where else in my code a leak  
could  
occur. if I ever populate arrays in my controllers or models, they all  
have  
method scope so in theory should be collected by the GC when the method  
returns. Controller classes are instantiated on each request in  
production  
mode, right?

Also, can anyone recommend some tools for detecting leaks other than the  
script mentioned above?

Cheers  
Ian

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [May 15, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/understanding-memory-leaks/104214/2 "2007-05-15T14:41:55Z")

</div>

On 5/15/07, Ian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Other than this situation, I fail to see where else in my code a leak could  
> occur. if I ever populate arrays in my controllers or models, they all have  
> method scope so in theory should be collected by the GC when the method  
> returns. Controller classes are instantiated on each request in production  
> mode, right?
> 
> Also, can anyone recommend some tools for detecting leaks other than the  
> script mentioned above?

Try Bleak House:

> **[bleak\_house](https://blog.evanweaver.com/2007/04/28/bleak_house/)**
>
> It’s bleak to have leaks. third update Please see here for up-to-date documentation. second update There is now a pure-C heap instrumentation as well as the Ruby/ObjectSpace one. You really should …

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![elliterate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@elliterate](https://www.ruby-forum.com/u/elliterate)\
**Post date:** [May 15, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/understanding-memory-leaks/104214/3 "2007-05-15T15:57:08Z")

</div>

Some interesting mailing list posts I’ve found interesting while  
inspecting my app are

[http://tinyurl.com/2qw6l5](http://tinyurl.com/2qw6l5)

[http://tinyurl.com/2rdq2m](http://tinyurl.com/2rdq2m)

Bleak house is definitely the tool to use, and is worth just to try to  
stress your app and see if the memory usage just keeps to grow or  
stops at some point.

Paolo
