# Memory leak

**URL:** <https://www.ruby-forum.com/t/memory-leak/105078>\
**Category:** Ruby\
**Created:** [May 25, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/memory-leak/105078 "2007-05-25T16:20:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/1 "2007-05-25T16:20:39Z")

</div>

Hi

I am currently learning to use Ruby-DBI and have a really simple script  
(pasted below). The problem I am having is that the program seems to be  
leaking memory! When monitoring it in the activity monitor (MacOSX) I  
can see that the process uses 100kb more memory every second! Why does  
this happen? I am planning to write a little daemon that is going to run  
non-stop on a server and can’t have it leak memory. Is it dbi that is  
leaking or is there something in my code?

If I let it run for 24 hours the process would take 8,6gb of memory  
instead of the 3mb it started out with, and my server isn’t up for that!

Hope you have any suggestions.

I also tried a patch to the ruby-mysql api which was supposed to handle  
some memory leak, but it didn’t improve my test scenario…  
([http://railsexpress.de/blog/articles/2006/10/05/make-ruby-mysql-create-less-garbage](http://railsexpress.de/blog/articles/2006/10/05/make-ruby-mysql-create-less-garbage))

#!/usr/bin/ruby -w

require “dbi”

while true

begin  
# connect to the MySQL server  
dbh = DBI.connect(“DBI:Mysql:test:localhost”, “root”, “”)  
# get server version string and display it  
row = dbh.select\_one(“SELECT VERSION()”)  
puts "Server version: " + row[0]  
rescue DBI::DatabaseError =\> e  
puts “An error occurred”  
puts “Error code: #{e.err}”  
puts “Error message: #{e.errstr}”  
ensure  
# disconnect from server  
dbh.disconnect if dbh  
end  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/2 "2007-05-25T16:27:05Z")

</div>

Why is it inside a while loop?

Aren’t you just creating hundreds and thousands of db connecions?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/3 "2007-05-25T16:27:41Z")

</div>

Well, never mind… it stops “leaking” after a while. When it is at  
about the double size of what it started out at. The first version I run  
paused one second between each run so I didn’t realize that till after I  
had posted my previous post. When removing the “sleep 1” (not in the  
source code I posted) I realized that the process doesn’t consume more  
memory when it has reached about 6mb.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/4 "2007-05-25T16:35:39Z")

</div>

Sebastian probst Eide wrote:

> Peter H. wrote:
> 
> > Why is it inside a while loop?
> > 
> > Aren’t you just creating hundreds and thousands of db connections?
> 
> Well, now, not really. I am closing the connections I am making which  
> should make it all right? Although having the connection part outside  
> the loop probably would make sense… But I am going to have the program  
> pause at certain intervals and didn’t want to have any open  
> connections… that is why I tried it like this… But opening and  
> closing a connection shouldn’t make the process memory consumption grow,  
> should it?

Well… ofcourse you were right. Moving the loop into the begin clause  
made the program a lot faster and lowered the memory consumption.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/5 "2007-05-25T16:30:55Z")

</div>

Peter H. wrote:

> Why is it inside a while loop?
> 
> Aren’t you just creating hundreds and thousands of db connections?

Well, now, not really. I am closing the connections I am making which  
should make it all right? Although having the connection part outside  
the loop probably would make sense… But I am going to have the program  
pause at certain intervals and didn’t want to have any open  
connections… that is why I tried it like this… But opening and  
closing a connection shouldn’t make the process memory consumption grow,  
should it?

Sebastian

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/6 "2007-05-25T16:47:07Z")

</div>

On 25.05.2007 16:27, Sebastian probst Eide wrote:

> Well, never mind… it stops “leaking” after a while. When it is at  
> about the double size of what it started out at. The first version I run  
> paused one second between each run so I didn’t realize that till after I  
> had posted my previous post. When removing the “sleep 1” (not in the  
> source code I posted) I realized that the process doesn’t consume more  
> memory when it has reached about 6mb.

IIRC DBI also supports the block form for opening and closing, so you  
should rather do

DBI.connect(“DBI:Mysql:test:localhost”, “root”, “”) do |dbh|  
…  
end

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/7 "2007-05-25T16:47:26Z")

</div>

Peter H. wrote:

> If my understanding is correct the garbage collector will only jump in  
> when things get dangerously low or when there is a pause(?) in the  
> process. So a tight loop can generate a large backlog of garbage that  
> needs to be cleaned up.  
> Ok, good to know! Thanks Peter!

Best regards  
Sebastian

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/8 "2007-05-25T16:45:07Z")

</div>

If my understanding is correct the garbage collector will only jump in  
when things get dangerously low or when there is a pause(?) in the  
process. So a tight loop can generate a large backlog of garbage that  
needs to be cleaned up.

Others will probably be able to inform this better than I.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/memory-leak/105078/9 "2007-05-25T16:50:34Z")

</div>

> IIRC DBI also supports the block form for opening and closing, so you  
> should rather do
> 
> DBI.connect(“DBI:Mysql:test:localhost”, “root”, “”) do |dbh|  
> …  
> end
> 
> robert

Thanks for the tip!  
I’ll use that instead.
