G'day crunch_this!,

From Test::LeakTrace:

"Leaked SVs are SVs which are not released after the end of the scope they have been created. These SVs include global variables and internal caches. For example, if you call a method in a tracing block, perl might prepare a cache for the method. Thus, to trace true leaks, no_leaks_ok() and leaks_cmp_ok() executes a block more than once."

When I tried your code with leaktrace(), a large number of leaks (~50) were detected; however, with no_leaks_ok(), I got none. This could mean I am unable to reproduce your problem. Please advise what OS and Perl version you're using. Can you also show your testing code. I'm using:

$ perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-th +read-multi-2level $ uname -a Darwin ganymede 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25: +48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

The most obvious source of a memory leak in the code you posted is in the innermost loop, where you declare an array and take a reference to it. Every iteration through that innermost loop creates a new array with its own memory; this memory will not be released until %haystack goes out of scope (you don't show that in your code). So, @quartic and @derivative are both created afresh in the innermost loop and their memory should be freed after exiting that loop; however, multiple @zeros are created and their memory is not released because a reference is stored in %haystack.

I'm going to suggest a partial solution whereby you don't create any arrays at all in the innermost loop; its partial because I don't know what you're doing with %haystack. The fact that you're not declaring and then assigning any arrays may give you a performance bonus: Benchmark will tell you.

foreach my $x (3 .. $rep ) { foreach my $y (2 .. $x-1 ) { foreach my $z (1 .. $y-1 ) { # expanded form of x*(x - a)*(x - b)*(x - c) # coeffs are in an array $haystack{"$x, $y, $z"} = [ poly_roots( poly_derivative( 1, -$x - $y - $z, $x*$y + $x*$z + $y*$z, -$x*$ +y*$z, 0 ) ) ]; } } }

[Side issue: You'll note I've changed $a, $b and $c to $x, $y and $z. $a and $b are special package variables and it's generally best not to use them except for their intended purpose (see perlvar for more details). $c is not special: I just changed that also for completeness. $x, $y and $z may not be the best choices (but $a and $b and definitely bad choices) — I'll leave you to decide what you want to call them.]

It would be helpful if you provided a self-contained script that showed:

If your output is particularly lengthy, you can wrap it in <spoiler>...</spoiler> or <readmore>...</readmore> tags (see Markup in the Monastery for details). For your self-contained script, you'll find guidelines in: How do I post a question effectively?

-- Ken


In reply to Re: help with memory leak by kcott
in thread help with memory leak by crunch_this!

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.