I have a program that's exhibiting what appears to be a memory leak, but I'm having trouble tracking it down. The test program I wrote to demonstrate this behaves rather strangely, and I want to make sure I understand why.
In the example there are two arrays, one named regexLeak and another named noLeak. The noLeak arrays is simply there as a baseline using two simple regexes. The regexLeak array contains to regexes that use a character set.
The test program takes the contents of an array, presumably filled with regex patterns, passes it to a function where the regexes are compiled and placed into an array that's passed as a reference back.
When the "test" array is set to noLeak, there is no leak. When the "test" array is set to regexLeak, there is a notable memory leak. However, if the regexLeak array only contains one element, there is no leak. It doesn't matter which element either. Put both elements in the array though and memory use shoots through the roof.
#!/usr/bin/perl
use strict;
use warnings;
my @regexLeak;
my @noLeak;
# Leaks only when both elements are added to @regexLeak
push(@regexLeak,'user = ([a-zA-Z0-9-_.@]+)');
push(@regexLeak,'admin = ([a-zA-Z0-9-_.@]+)');
# No leaks
push(@noLeak, 'simpleRegex');
push(@noLeak, 'anotherSimpleOne');
my @test = @regexLeak;
my $compiled = compileRegex(@test);
while (1) {
$compiled = compileRegex(@test);
# print scalar @{$compiled}."\n";
# select(undef, undef, undef, 0.25);
}
sub compileRegex {
my (@r) = @_;
my @compiled;
foreach my $regex (@r) {
my $c = qr/$regex/;
push(@compiled,$c);
}
return \@compiled;
}
Some elements of this were taken from a production program that's exhibiting this problem. The global $compiled variable, for example. I'd like to keep this in here as explaining through them would help me understand. This is also a long running program so leaks are a concern.
Memory leak was observed using ps -aux and reviewing RSS and VSZ sizes.
Any help or guidance would be appreciated, thanks!
Edit: If you need any other environment details let me know
Perl v5.24.0 built for x86_64-linux-thread-multi