Now in theory it could happen that you have an array of a handful elements and after 1 million combined push and shift operations it allocates megabytes of space while not logically growing. ... I doubt this happens

That's my position also. And I was bored, so here's the proof.

#!/usr/bin/env perl #===================================================================== +========== # # FILE: fifo-mem.pl # # USAGE: ./fifo-mem.pl # # DESCRIPTION: Test memory consumption of a push/shift FIFO # See https://www.perlmonks.org/?node_id=11157497 # #===================================================================== +========== use strict; use warnings; use Memory::Usage; my @fifo; my $max = 100_000_000; my $mu = Memory::Usage->new; $mu->record ('start'); for my $count (1 .. $max) { push @fifo, 'dog'; shift @fifo; $mu->record ("After $count iterations") unless $count % 10_000_000 +; } $mu->record ('finish'); $mu->dump;

And since not everyone can run this, here's the output:

$ time vsz ( diff) rss ( diff) shared ( diff) code ( dif +f) data ( diff) 0 10780 ( 10780) 6544 ( 6544) 5448 ( 5448) 4 ( 4) + 1356 ( 1356) start 3 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 10000000 iterations 5 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 20000000 iterations 8 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 30000000 iterations 10 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 40000000 iterations 12 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 50000000 iterations 15 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 60000000 iterations 17 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 70000000 iterations 20 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 80000000 iterations 22 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 90000000 iterations 24 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) After 100000000 iterations 24 10780 ( 0) 6544 ( 0) 5448 ( 0) 4 ( 0) + 1356 ( 0) finish $

As expected, no leak whatsoever.

It could also be an issue if the OS doesn't "take back" released memory.

Of course, but so could any program doing anything. That doesn't constitute a leak.


🦛


In reply to Re^3: How to implement a Queue that doesn't leak memory? by hippo
in thread How to implement a Queue that doesn't leak memory? by Anonymous Monk

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.