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.