Do you know where your variables are? | |
PerlMonks |
Re: help with memory leakby kcott (Archbishop) |
on Apr 17, 2013 at 05:34 UTC ( [id://1029064]=note: print w/replies, xml ) | Need Help?? |
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:
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.
[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 Section
Seekers of Perl Wisdom
|
|