in reply to Memory Leak with XBase?

I'm not familiar with Xbase, and I'm sure that Tachyon is right on. But I would like to make a general comment on your code, because I've been seeing this alot in my own trials.

As a general rule, don't use "new" in a loop unless you want to keep them. For instance, in your code I would do something along the following:

my $dbname = "dbname.dbf"; my $table = new XBase "${dbname}") || die; while(1) { for my $i (1 .. 100) { #pseudo code $table->empty; $table->read($_); $table->display; } sleep(2); } $table->empty;
That way you are reusing your $table object, instead of wastefully creating new ones for each pass.

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Re: Memory Leak with XBase?
by tigervamp (Friar) on Mar 19, 2004 at 17:15 UTC
    Absolutely, but I can't do that here. The main issue here was that I needed to check for newly appended records very often and this can only be done by closing/opening the table with new to re-read the dbase header and access the new records. I was able to slightly modify the XBase.pm module to allow safely calling the read_header function on an open table whenever I like. Now I can just open the needed tables once at the begining of the program, store the table objects in a hash, and just call read_header to check for new records instead of closing/opening. This has solved my memory issues.