# Memory leaks, maybe. need help

**URL:** <https://www.ruby-forum.com/t/memory-leaks-maybe-need-help/170349>\
**Category:** Ruby\
**Created:** [June 16, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/memory-leaks-maybe-need-help/170349 "2009-06-16T14:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@doron](https://www.ruby-forum.com/u/doron)\
**Post date:** [June 16, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/memory-leaks-maybe-need-help/170349/1 "2009-06-16T14:05:49Z")

</div>

I am writing a server using ruby 1.8.6 patchlevel 287. It connects to  
another server, fetches and process information.  
Sometimes it needs to process a lot of information. It uses net/http  
and REXML libraries.  
My problem is that at some point the process memory usage grows from  
20MB to 300MB.

I used gdb and rb\_object\_counts to look at the number of objects it  
uses.  
While the memory usage is low i see it uses 20K Strings, 1k or Arrays  
etc.  
While the memory usage is high i see it uses 1M Strings, 200k or  
Arrays etc.  
After a while, when the load is over, it again uses only 20K string  
and 1K Arrays. It seems that GC worked fine.  
But the memory usage I see using top is still around 300MB.

Does it mean I have memory leaks?  
I read somewhere that “MRI’s garbage collector never returns memory to  
the system”. that can explain what I see, but is it true? If so, is  
there any solution?

---

<div class="post-metadata">

**Author:** ![doron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@doron](https://www.ruby-forum.com/u/doron)\
**Post date:** [June 16, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/memory-leaks-maybe-need-help/170349/2 "2009-06-16T19:54:32Z")

</div>

> Does it mean I have memory leaks?  
> I read somewhere that “MRI’s garbage collector never returns memory to  
> the system”. that can explain what I see, but is it true? If so, is  
> there any solution?

MRI returns memory to the system if it is alloc’ed and free’ed  
besides that it allocates memory in ever increasing heap sizes, which  
means that they have less and less of a chance to be reclaimed [though  
in 1.9 they are 2K each so less of a problem…I guess].  
Anyway, to the point–try 1.9 or 1.8.x with MBARI patches they are much  
better with memory [or jruby].  
-=r

---

<div class="post-metadata">

**Author:** ![doron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@doron](https://www.ruby-forum.com/u/doron)\
**Post date:** [July 4, 2009, 12:08am UTC](https://www.ruby-forum.com/t/memory-leaks-maybe-need-help/170349/3 "2009-07-04T00:08:28Z")

</div>

On Tue, Jun 16, 2009 at 12:54 PM, Roger P.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> MRI returns memory to the system if it is alloc’ed and free’ed  
> besides that it allocates memory in ever increasing heap sizes, which  
> means that they have less and less of a chance to be reclaimed [though  
> in 1.9 they are 2K each so less of a problem…I guess].  
> Anyway, to the point–try 1.9 or 1.8.x with MBARI patches they are much  
> better with memory [or jruby].

I’d definitely recommend trying JRuby. There’s a few knobs you can  
turn to keep memory under a specific size (default is to let it grow  
no larger than 512MB heap, but many apps will run in a lot less) which  
means it won’t use up all available memory unless you tell it to. And  
though it uses more memory out of the box, if you have a large dataset  
or heavy object churn and high loads, JRuby does extremely well.

- Charlie
