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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.