I've written an http server, much of it using Inline::C, and I have a memory leak I cannot pin down.

Every few score requests, mem usage grows by exactly 132 kB. By "every few score" I mean irregularly, and on average. Sometimes it happens after 10 requests, sometimes 100. The requests are all identical, generated via apachebench.

I do not believe this is because of anything explicit in the C routines since there is only one malloc call, for a file slurp, which in this test would only be a few hundred bytes, the pointer is free'd a few lines later after its content is assigned to an SV, and such a leak would grow in a regular linear fashion every time a new memory page was needed.

132 kB is a power of two value so I suspect it is some data structure perl is growing. However, I cannot find anything leaky in my perl code either -- I have checked for circularity with Test::Memory::Cycle, and I have a recursive rountine for checking reference counts of objects/structures and their members before they are deleted or undef'd.

I've tried valgrind, but it is almost useless for me with perl since even "helloworld.pl" reports thousands of kB remaining in hundreds of blocks.

Anyone know a way I can trace allocation, hopefully in real time?

later: I did find one stray reference that accounts for the major leak, however I still have the same issue on a much smaller scale -- the increase is now every 1000-2000 requests. See below http://www.perlmonks.org/?node_id=925997 for how I am tracing references.

finally: I found the smaller leak by watching PL_sv_count as per Dave's suggestion, qv. http://www.perlmonks.org/?node_id=926128. No more leaky http server!


In reply to Tracing memory leak by halfcountplus

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.