Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Memory leak

Reply
Thread Tools

Memory leak

 
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
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...e-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:atabaseError => 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      05-25-2007
Why is it inside a while loop?

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


 
Reply With Quote
 
 
 
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
Peter Hickman 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
Sebastian probst Eide wrote:
> Peter Hickman 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.


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-25-2007
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
 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      05-25-2007
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.


 
Reply With Quote
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
Peter Hickman 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sebastian probst Eide
Guest
Posts: n/a
 
      05-25-2007
> 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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Memory leak??? (top reporting high memory usage under Solaris) Mark Probert Ruby 4 02-09-2005 06:13 PM
Wireless Zero Configuration Memory Leak?? =?Utf-8?B?Umlja3NjaHVsdHox?= Wireless Networking 3 01-19-2005 11:26 PM



Advertisments