On Wed, 2002-05-29 at 15:26, drieux wrote:
> 
> On Wednesday, May 29, 2002, at 09:06 , Jackson, Harry wrote:
> 
> >> -----Original Message-----
> >> From: F.Xavier Noria [mailto:[EMAIL PROTECTED]]
> >>
> >> What would be the shortest code that leak?
> >
> > Its not really the length as much as the width of the whole that causes a
> > leak.
> 
> 
> well you could write a self referential function
> 
>       sub sillyMe {
>               my ($arg ) = @_
>               my $value = $arg + time + 1;
>               sillyMe($value);
>       }
> 
>       sillyMe(1);
> 
> 
> that wouldn't really 'leak' but it would buy you a
> stack frame for each call - and some arguments each
> time through until you ran out of heap....
> 
> ciao
> drieux


One way to create a memory leak (at least while a script is running) is
to use circular references:

<code>
#!/usr/bin/perl

use strict;

for (0..1000000) {
        my $a = 8;  #$a's reference count is set to 1
        my $b = 10; #$b's reference count is set to 1
        #$a leaves scope so is reference count is decremented by 1
        #$b leaves scope so is reference count is decremented by 1
} #$a and $b's reference counts are 0 so the get GC'ed

system("ps -o rss,pid,cmd | grep $$ | grep -v grep");

for (0..1000000) {
        my $a; #$a's reference count is set to 1
        my $b; #$b's reference count is set to 1

        $a = \$b; #$b's reference count is incremented by 1
        $b = \$a; #$b's reference count is incremented by 1
        #$a leaves scope so is reference count is decremented by 1
        #$b leaves scope so is reference count is decremented by 1
} #$a and $b's reference counts are both still 1 so they don't get GC'ed

system("ps -o rss,pid,cmd | grep $$ | grep -v grep");

#program ends and the real GC code cleans up the mess
</code>

<output>
1224  7565 /usr/bin/perl ./hog.pl
33108 7565 /usr/bin/perl ./hog.pl
</output>

-- 
Today is Prickle-Prickle the 3rd day of Confusion in the YOLD 3168
Kallisti!

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to