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)
In reply to Substitution with regex and memory consumption by k-mx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |