Today @$work we discovered that even dummy recursive calling of a function is leaking memory. Here is the one-liner 10000x calling it self and returning:
perl -MEnglish -MGTop -le 'my $g=GTop->new();$m=$g->proc_mem($PID);print $m->size; sub r { return if not $_[0]; r($_[0]-1); } r(100000); $m=$g->proc_mem($PID); print $m->size;'
The output is:
7122944
34729984
Before the recursion 7MB allocated. After the recursion (that finished) 34MB...
Here is what Test::LeakTrace say about it:
$ perl -MTest::LeakTrace -le 'sub r { return if not $_[0]; r($_[0]-1); } leaktrace{ r(1); };'
leaked ARRAY(0x9b7e8e8) from -e line 1.
leaked SCALAR(0x9b98cb8) from -e line 1.
leaked ARRAY(0x9c311f8) from -e line 1.
leaked SCALAR(0x9c311e8) from -e line 1.
Are we doing anything wrong? Is it ok? How to release the memory?
Re: sub r {return if !$_[0]; r($_[0]-1); } leaktra (Score:1)
Re: (Score:1)
perl -MTest::LeakTrace -le 'sub r {return if !$_[0]; r($_[0]-1); } r(1); leaktrace{ r(1);};'Hmm yes the second recursion calling is not leaking memory any more.