http://qs1969.pair.com?node_id=218495

ph0enix has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

In one of my app I'm using hash of objects. Each object hold it's own data as key - value pairs which are tied to the PostgreSQL database via Tie::RDBM module. Tieing to database is used to minimize physical memory requirements.

My problem is memory greediness. When I iterate through all key - value pairs there is allocated new additional memory which is not released... :( Function which iterate through key - value pairs is method of this object.

package Elric::Lexicon; sub new { # so a lexicon is basically a hash (of entries) return bless {}, shift; } sub info { my $self = shift; my $num_keys = 0; my $num_mu = 0; my $key; my $value; &message("Buckets used/allocated:\t".scalar(%{$self})."\n"); while(($key,$value) = each %$self) { $num_keys++; $num_mu += scalar(@$value); } return $num_mu; } sub bind { my $self = shift; my $type = shift; my $lang = shift; if ($type eq 'rdbm') { my $esd = \%Elric::System::default; # test if connectdb called before if (!defined $$esd{db}->{pass}) { message("error: can't bind... use connectdb first\n"); return 0; } eval "use Tie::RDBM;" if ! defined $INC{'Tie::RDBM.pm'}; tie (%$self, 'Tie::RDBM', { db => "dbi:$$esd{db}->{type}:". "dbname=$$esd{db}->{name};". "host=$$esd{db}->{host};". "port=$$esd{db}->{port};", user => $$esd{db}->{user}, password => $$esd{db}->{pass}, table => $lang, create => 1, drop => 1, autocommit => 1, DEBUG => 0 }) or die $!; } return 1; } ...

This code is called from the main program

use Elric::Lexicon; my %lexicon = (); ... $lexicon{'EN'} = Elric::Lexicon->new; $lexicon{'EN'}->bind('rdbm', 'EN'); ... my $mind_units = 0; foreach (sort keys %lexicon) { # iterate all present lexica &message("Lexicon $_:\t"); # name the current lexicon $mind_units += $lexicon{$_}->info(); # print information about it +s internals }

Any suggestions welcome