# memory used increase 2000k, undef $Template; #the memory did not released

How are you determining the memory usage here?

If you are using a tool like ps or top to view the memory usage of the perl process, then you will get this result. When perl requests memory from the operating system via malloc, this memory is not returned until the process dies. When perl is finished with the memory is is made available (with free) in the heap so that further memory allocations via malloc will use it again until it is exhausted (at which time it will go to the operating system again for more) but it is not returned to the operating system.

Implementing a malloc-style memory allocator that does return memory to the operating system upon free would be complicated and it would be prevented from returning memory in lots of cases anyway. This is because the freed memory is likely to be surrounded by other memory that has not yet been freed, or because memory can only be freed in full pages (typically 4 or 8 kibibytes) and there is other stuff on the same page that is still required.

The worst that will happen anyway if you have a process that has freed lots of memory but the process keeps on living for a long time such that the useless memory held by the process forces the whole system to eventually run low on memory is that the chunks that have been freed will be swapped out to disk (they would be chosen as good candidates for swapping by the operating system since they have not been referenced in a long time) and will never be swapped in again. If the operating system is doing a good job then the pageout will happen a little bit before it is needed, so no process will be forced to block and wait for it. That's almost no performance penalty.

If you think instead that the memory occupied by $Template has not been freed by perl, then you must assume that someone is still holding a reference to it. That reference could be in a package variable or a toplevel-scoped lexical in the Text::Template class for example. You'd have to check the implementation. But I don't think this is what's going on here.


In reply to Re: memory leak by Celada
in thread memory leak by camenix

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.