in reply to Re: Re: Re: Re: eval with sub
in thread eval with sub

A Perl program can indeed leak memory if you accidentally (or intentionally!) set up circular references. Perl uses a "reference counting" scheme for tracking objects. Objects are reclaimed (garbage collected) when their reference count goes to zero. If you set up a pair of objects to reference each other, their reference counts will never go to zero, even after the variables that originally pointed to them have gone out of scope.

There's a bit more discussion of the in perlman:perltoot.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: eval with sub
by merlyn (Sage) on Mar 10, 2001 at 16:22 UTC
    And besides that, there's also apparently a leak in the compiler, so repeated compilation continues to leak. I think it shows up particularly if the compiler has to abort in the middle, and less so if the compiler completes its task on a compilation unit.

    Hence, repeated runtime string-eval is bad. Don't do it.

    -- Randal L. Schwartz, Perl hacker

Re: Re: Re: Re: Re: Re: eval with sub
by sierrathedog04 (Hermit) on Mar 10, 2001 at 17:23 UTC
    The perl manuals state that:

    "The only situation where Perl's reference-based GC won't work is when there's a circularity in the data structure, such as:
    $this->{WHATEVER} = $this;"
    One can easily imagine such a self-reference. For instance, an object-oriented tax program might include a reference to one's employer. If one is self-employed then a self-reference could result.

    It is still not immediately clear why Merlyn says that using eval in the situation he describes also creates a memory leak. I guess that the lesson for new monks in this thread is to avoid eval when it can be avoided, since it can have unknown deleterious effects on memory management.