Memory leak

  • Thread starter Sebastian probst Eide
  • Start date
S

Sebastian probst Eide

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)

#!/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
 
P

Peter Hickman

Why is it inside a while loop?

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

Sebastian probst Eide

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.
 
S

Sebastian probst Eide

Peter said:
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
 
S

Sebastian probst Eide

Sebastian said:
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.
 
R

Robert Klemme

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
 
P

Peter Hickman

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.
 
S

Sebastian probst Eide

Peter said:
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
 
S

Sebastian probst Eide

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top