Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Memory Management and Array references

by chromatic (Archbishop)
on May 25, 2004 at 17:15 UTC ( #356299=note: print w/replies, xml ) Need Help??

in reply to Memory Management and Array references

It's not a memory leak. Perl is doing exactly what you tell it to do. When you're finished with $self->{files}, delete it.

Replies are listed 'Best First'.
Re: Re: Memory Management and Array references
by crabbdean (Pilgrim) on May 25, 2004 at 21:05 UTC
    I'm sorry but that's not helpful at all. I showed an awareness that the problem was because of how I coded it and how Perl was handling the Memory Management based on how I coded it. $self->{files} is deleted when object goes out of scope. Its the abandoned references to the arrays within the $self->{files} that is the problem. Can you offer any advice on how to solve that problem? That would be helpful.

    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

      Any structure will be freed in perl when the last reference to it goes away; in your iterator, when you've exhausted the top-level directory and start working on the next you have:

      if( opendir( DIR, $dir ) ) { $self->{files}= [ map { File::Spec->catfile( $dir, $_ ); } File::Spec->no_upwards( readdir(DIR) ) ]; closedir DIR; } else { ... {

      At that point, when you assign a new arrayref to $self->{files}, the previous arrayref in that spot is overwritten and therefore its reference count is reduced - if that was the only reference to the previous arrayref, it will automatically be freed.

      So, to free unneeded memory in perl all you need to do - all you should do - is precisely to remove all references to the data.

      I suspect therefore that the problem is not where you think it is. I suggest that you step back and compose a new question - start off by telling us what makes you think you are leaking memory, and what in the process of tracking that down led you to believe the problem lay here.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://356299]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-12-17 02:16 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (71 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!