Hi Brothers,

I have written a script that parses 20 million+ customer transaction records stored in text files. These text files are extracts from various source systems. The purpose is to find out who are the top 30 customers, and extract details from these customers.

The following is extracted from my main module -
... # 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; }
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.

I dug up the following node http://www.perlmonks.com/index.pl?node_id=126591 from Perl Monks. Towards the end of the discussion node, Tye recommended restarting the process with exec { $^X, $0, @ARGV }.

Ummm, what does exec { $^X, $0, @ARGV } do? I have a feeling that if I do the exec, I will lose all the variables. Is that right?

Ah, this suddenly gave me an idea - what if I do the following:
... # 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); ...
I haven't tried the fork yet. But my gut feeling is that this will not work. I can think of two scenarios:

Senario 1 - by killing the parent process, the child process will die with it???

Senario 2 - Ok, perhaps the child process will not die, but it will inherit all the virtual space from the parent, which means all these efford for nothing???

Oh, dear brothers, what shall I do? Can you please give me some advice on how to free up process memory?

Thanks you very much, your help is greatly appreciated!

In reply to Force perl to release memory back to the operating system by Roger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.