#!/usr/bin/perl -w use strict; use warnings; sub myfunc_1 { my $A = ''; vec($A, 20000000, 8) = 0; return $A; } sub myfunc_2 { vec($_[0], 100000000, 8) = 0; return 0; } $b = ; my $BIGSTRING = ''; myfunc_2($BIGSTRING); # memory usage is normal. $b = ; undef $BIGSTRING; # Pff! Gone from memory! TINYPERL.EXE memory # usage visibly shrinks in Windows Task Manager. $b = ; my $STRING2 = myfunc_1(); # memory usage is double! $b = ; undef $STRING2; # Deletes one copy only $b = ; #### #!/usr/bin/perl -w use strict; use warnings; sub MemoryEaterFunc { my $A = ''; vec($A, 20000000, 8) = 0; # Create a large string $A .= $_[0]; # Add to it. Do something. # Evaluate the last statement, # and that is the return value of the sub! even if # you don't write return $A; it still returns it. # If you want the function to not return the big string, # then put 'return 0;' at the end of the sub. } $a = ; MemoryEaterFunc(2); # Eats memory and doesn't release it $a = ; #### sub MemoryEaterFunc_FIXED { my $A = ''; vec($A, 20000000, 8) = 0; # Create a large string $A .= $_[0]; # Add to it. Do something. undef $A; }