http://www.perlmonks.org?node_id=266839

RobCheung has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, Does Perl have garbage collection mechanism and how it performs?
#----------------------------------------------------------- use 5.008; use strict; use threads qw(yield); use threads::shared (); use Time::HiRes qw(sleep); our @GlobalArray : shared = (1..1000); my $sub1 = sub { while (1) { my $a; { lock @GlobalArray; $a = shift @GlobalArray; } print $a, "\n"; yield; sleep 0.05; } }; my $t1 = new threads $sub1; my $t2 = new threads $sub1; my $t3 = new threads $sub1; $t1->detach; $t2->detach; $t3->detach; while (1) { # readFile(); # ... push @GlobalArray, (1..1000) unless scalar @GlobalArray; sleep 0.001; } 1; #-----------------------------------------------------------
This is a simple example, but it has a serious problem (on Windows 2000) that the used memory will increase by 200kb after every "push", and it will be out of memory soon. I wonder that how the GC mechanism performs? Is there any way to fix the problem by collect some leaked memory during the runing time? Thanks advance for any help!