[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Memory problems in OO context



On Mon, Nov 29, 1999 at 05:50:56PM -0600, Matthew Langford wrote:
> On Mon, 22 Nov 1999, Peter Hartmann wrote:
> 
> > >|...I don't see how I could possibly free up more memory. Am I doing
> > >|something wrong here?
> > >
> > >Yes. As Gisle's note says, you have to *explicitly* delete the html parse
> > >tree. Just undefing it doesn't do it, that's *implicitly* deleting it,
> > >which doesn't work because of circular references. Replace the undef $html
> > >with $html->delete and see if that changes the behavior at all. (And yes, I
> > >think that could be stated more clearly in the HTML pods.)
> > 
> > Yes, this works like a charm. There is still some memory loss in the realm
> > of an estimated 10K per iteration, but with this tiny change I was able to
> > drop the whole bunch of about a hundred files on the droplet with out
> > making MacPerl crash on me for lack of free memory. (Hobby programmers like
> > me should be prohibited from tackeling OO stuff....)
> 
> 
> Unfortunately, this doesn't seem to solve my problem.  I wasn't using the
> HTML::Tree mechanism, and I don't believe ::Parser and ::Filter have
> circular references.
> 
> I undef $p (my filter object instance), checking ref $p before and after.
> Before it is true, after it is false, which seems like Perl should then be
> able to reclaim it.
> 

It seems that you have not yet fully grasped the concept of circular
references.

Let's say you build a structure like this:

$x = [ 0, 1, 2 ];
push @$x, $x;

Now the anonymous array contained in $x has a reference to itself:

  $x --+->  [ 0, 1, 2, * ]
       |_______________|

Then you undef $x:

undef $x;

which leaves you with this:

  $x   +->  [ 0, 1, 2, * ]
       |_______________|

Now, the value of $x is undefined, and ref $x of course returns false.
But, there is still a reference to the anonymous array (which happens to be
the last element of the array), so Perl cannot reclaim that memory.

Simply deleting one of the references to a data structure is not enough to
free it; you must delete _all_ the references, which in this case means
breaking the circular reference _before_ you call undef $x.

Whether this applies to the modules you are using, I don't know, but the
continual climb in memory usage certainly suggests that it does.


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org