As you can see, at the end of the sub "GetTopCustomers" I tried to "force" the garbage collector to work. But of course this is not going to work, as the perl processor does not give up its virtual memory until the process terminates. But I really want to free up memory for this process, because when it gets to the end of "GetTopCustomers", the process has eaten up ~2Gb of memory.... # Find top 30 customers # Build a hash for top 30 customers in the # form of { 'cust1' => balance1, 'cust2' => balance2, ... } my $top30_customers = &GetTopCustomers(\@input_files, $fx_rates, 30); # Retrieve details about these top 30 customers my $customer_details = &GetCustomerDetails(\@input_files, $top30_customers); ... sub GetTopCustomers() { my ($inputfiles, $fx_rates, $nth) = @_; my %balances; ... foreach my $file (@{$inputfiles}) { my $f = new IO::File $file, "r"; ... while (<$f>) { # $cust, $bal, $ccy are extracted from each line ... $balances{$cust} += $bal * $fx_rates->{$ccy}; ... } } ... # Sort the customers by descending order of balance my @sorted_cust_list = reverse map {$_->[0]} sort {$a->[1]<=>$b->[1]} map{[$_,$balances{$_}]}keys %balances; my %topNth = map {$_=>$balances{$_}}(@sorted_cust_list)[0..$nth-1]; # Try to force the garbage collector to work ?@$!%# undef %balances; undef @sorted_cust_list; return \%topNth; }
I haven't tried the fork yet. But my gut feeling is that this will not work. I can think of two scenarios:... # Find top 30 customers # Build a hash for top 30 customers in the # form of { 'cust1' => balance1, 'cust2' => balance2, ... } my $top30_customers = &GetTopCustomers(\@input_files, $fx_rates, 30); # Try to free up some memory here by starting # a child process. my $pid = fork() or die("Can not do fork!); if ($pid) { exit(0); # kill the parent process } # Retrieve details about these top 30 customers my $customer_details = &GetCustomerDetails(\@input_files, $top30_customers); ...
In reply to Force perl to release memory back to the operating system by Roger
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |