in reply to Re: Re: DBD::CSV Memory Leak
in thread DBD::CSV Memory Leak

In taking a look at your scratchpad, I don't immediately see the cause of your memory leak, but I see something that should help.

At the top of your code, you've predeclared all of your variables. This means that, regardless of where they are used, they exist. For example, you only use $hd_key in your &Setup_Config subroutine. This means that you should only declare it there, and preferably (though some argue this point) you should only declare it right before you use it. This means that all variables should have the smallest possible scope. You have, in effect, used global variables and eliminated part of the benefit of using strict. Consider the following example:

my $foo; sub bar { # lots o' code $foo++; # more code } sub baz { # do a few laps around the pool ($foo) = (split)[2]; # shower and go home }

In the above example, we've reused the variable $foo. For what good? Is it really necessary? This will confuse someone coming to maintain it later. Further, when the subs exit, $foo hangs around when it may not need to. Here's an example of a nice variable declaration:

sub versive { # some pushups while ( my ($key,$value)= each %data ) { # process things } # }

In that example, $key and $value exist only in the while loop. This means that the memory they use is available for reuse immediately after the scope of the loop ends (IIRC, the memory is not released back to the OS) and there is no worry about accidentally reusing one of those variables later and possibly having an incorrect value. If you get in the habit of coding like that, your code will be much easier to work with in the long run.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.