On the assumption that an RDBMS isn't in the cards, let's look at where you're chewing through memory, and see if we can't figure out a way to get by with less.
$balances{$cust} += $bal * $fx_rates->{$ccy};
is central to what you're doing. Unless there's some heuristic you can apply to exclude some customers (i.e., "forget these, since they'll never be anywhere near the top N"), you may be stuck here. How many customers do you have?

Moving on,

my @sorted_cust_list = reverse map {$_->[0]} sort {$a->[1]<=>$b->[1]} map{[$_,$balances{$_}]}keys %balances;
is the second hit, and it's a big one. First, keys %balances builds a big array, then you build another one via sort, then another via reverse. (Not all of the arrays are live all the way through the pipeline, but still...) There's a way around this, though. Instead of using a Schwartzian transform, make a low-footprint pass through the hash, using
while ( my($cust,$balance) = each %balances ) { ... }
and keep a smaller data structure that records the top N customers by balance. (I'll leave the choice of algorithm as an exercise for the motivated reader.) This is a bit more work, but it might run faster given the reduction in memory demands and attendant reduction in swapping/thrashing.

At this point, the big memory sink, assuming you have lots of customers, is %balances. You can undef this at the end of the subroutine, but, as far as I know, this won't result in Perl releasing memory back to the OS (and it goes away at the end of the routine anyway). The last time I looked into it, which was a few releases of Perl ago, Perl indirectly used a "raise the watermark if necessary" memory allocation strategy, with no provision for ever lowering the watermark.


In reply to Re: Force perl to release memory back to the operating system by dws
in thread 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.