#!/usr/bin/perl use strict; use warnings; $| = 1; print "\nSTEP 1: Reserve memory\n"; #my @GLOBAL_DATA; #$GLOBAL_DATA[0] = ''; #vec($GLOBAL_DATA[0], 9999999, 8) = 44; # Reserves just the right amount. #$GLOBAL_DATA[1] = 'x' x 10000000; # Reserves 20000000 bytes #$GLOBAL_DATA[2] = 'y' x 100000; # Reserves 200000 bytes #$GLOBAL_DATA[3] = 'z' x 1000; # Reserves 2000 bytes my $AA = 'H'; vec($AA, 9999999, 8) = 44; # Reserves just the right amount. #my $BB = 'g' x 10000000; # Reserves double memory!!! $a = ; print "\nSTEP 2: Do something with the data\n"; DoSomething(\$AA); # Pass by reference. PrintIt(\$AA); # Pass by reference. $a = ; print "\nSTEP 3: Free up memory\n"; undef $AA; # Frees up all of it #undef $BB; # Frees up only half of it! #undef $GLOBAL_DATA[0]; # Frees up all of it #undef $GLOBAL_DATA[1]; # Frees up only half of it! #splice(@GLOBAL_DATA, 1, 1); # Frees up half $a = ; print "\nSTEP 4: Pause\n$a"; exit; sub DoSomething { my $X = $_[0]; for (my $i = 0; $i < 60; $i++) { vec($$X, $i, 8) = $i + 48; # This operation doesn't use more memory } for (my $i = 9990000; $i < 9999999; $i++) { vec($$X, $i, 8) = 65; # This operation doesn't use more memory } print "\nDONE."; } sub PrintIt { my $X = $_[0]; print "\nLENGTH OF STRING = ", length($$X), "\n"; print "\nPREVIEW = ", substr($$X, 0, 60), "\n"; }