k-mx has asked for the wisdom of the Perl Monks concerning the following question:
Hello, comrades monks!
I want to share knowledge about interesting regex behavior:
#!/usr/bin/env perl use strict; use warnings; use 5.022; # WARNING! Will consume about 3GB of RAM $| = 1; my $size = 1024 * 1024 * 1000; my $s = 'C' x $size; # eval << 'EOD'; $s =~ s/C/1/; # copy of CCCCC... will stay in memory $s =~ s/C/2/; # same but 1CCCC... # EOD print "Time to measure memory usage...\n"; sleep(9000);
Every expression with substitution call, will copy original string and store it until next call. So, memory not leaking and will be reused (for e.g. inside loop blocks), but this behavior can lead to serious RAM consumption. The only known (for me) workaround for this problem is string eval, that will force memory reclaim, but this is clumsy in my opinion.
What do you think about such behavior? Is there more elegant way to free up memory used by 's///'?
Reproduced on 5.22.2 (Slackware 14.2), 5.30.0 (Centos 7), and no memory waste on 5.16.3 (Centos 7)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Substitution with regex and memory consumption
by dave_the_m (Monsignor) on Feb 29, 2020 at 20:27 UTC | |
by k-mx (Scribe) on Mar 01, 2020 at 09:19 UTC | |
by dave_the_m (Monsignor) on Mar 01, 2020 at 22:47 UTC | |
by k-mx (Scribe) on Mar 08, 2020 at 08:16 UTC | |
by dave_the_m (Monsignor) on Mar 08, 2020 at 11:51 UTC |