Topic: how to close db connection in proper way

I have tried small ruby program
require 'rubygems'
require 'postgres'
class DbAdapterTest
  def initialize
    puts "Initial memory"
    system("free -m")
    db = PGconn.connect('localhost', 5432, '', '', 'prdjnj_latest','postgres', 'postgres')
    puts "\n\n After establish database connection"
    system("free -m")
    res = db.exec("select * from offtake_reporting_tables limit 30000")
    puts "\n\n After execute query"
    system("free -m")
    db.close
    db=nil   
    puts "\n\n After close connection *****************************"
    system("free -m")
    a=1
    while a!=2
    end
  end
end
DbAdapterTest.new

Output
---------
Initial memory
                  total       used       free     shared    buffers     cached
Mem:          1000        900        123        0         23           378
-/+ buffers/cache:       498        511
Swap:         1223           0       1223

After establish database connection
                  total       used       free     shared    buffers     cached
Mem:          1000        900        123        0         23           378
-/+ buffers/cache:       499        510
Swap:         1223           0       1223

After execute query
                  total       used       free     shared    buffers     cached
Mem:          1000        933         80        0         23            380
-/+ buffers/cache:       529        474
Swap:         1223           0       1223

After close connection *****************************
                  total       used       free     shared    buffers     cached
Mem:          1000        932         81        0         23           380
-/+ buffers/cache:       528        475
Swap:         1223           0       1223

    Here free memory is gradually reduced from 510MB to 474MB when query is running..
After execution of a query,memory consumption is stopped that's free memory does n't decrease when running the 'while' condition.
The used memory should release when i closed the db connection but the used memory is not released even disconnect the db connection.
It is  released only when terminate the program.
Example
    36MB is consumed when query is running.i wise to release this memory after closed the db connection
Pls give me a solution to solve this program.

Last edited by willm (2010-05-13 01:30:56)