... # 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; } #### ... # 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); ...