Firstly, I'm not a Dancer user. What follows is just a suggestion for something to try; it may not help at all!

In each of your coderefs (get ... sub { ...} [1 instance] and ajax ... sub { ...} [2 instances]), you have code like this:

my @messages = (); ... template '...' => {messages => \@messages};

When they (implicitly) return, @messages will not be garbage-collected while the reference (\@messages) still exists.

I don't know why that reference might still exist. I followed it through Dancer (1.3116) source:

sub template { Dancer::Template::Abstract->template(@_) }

and then through Dancer::Template::Abstract (1.3116) source:

sub template { my ($class, $view, $tokens, $options) = @_; ...

but then, not knowing what templating engine you were using, was unable to continue. I'll leave that as an exercise for you.

You can ensure @messages is garbage-collected by using this code instead (which contructs an anonymous arrayref):

my @messages = (); ... template 'XXX' => {messages => [ @messages ]};

Expanding @messages will incur some overhead but that appears to be very small. Also, you might just be deferring your memory leak problem.

Be aware that memory may not be leaked on every cycle: a linear increase in memory usage may not be occurring at all. It might happen in code that's only run conditionally or perhaps as a result of some exception.

-- Ken


In reply to Re^3: How to deal with the fact that Perl is not releasing memory by kcott
in thread How to deal with the fact that Perl is not releasing memory by carlbolduc

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.