I have written a program to parse a custom application log file. It comprises of "events" that I am interssted amongst other junk that I am not. Each event has a number of attributes and the script it written to accomodate new attributes that are are added from time to time (which happens in the application frequently) to avoid code modification.

The storage mechanism used is associative arrays where the key used is the event number catenated with a "|" and the attribute number (e.g. $event_number."|$attribute_number"). All events are read into memory and then (at the end of reading the file) are sent to a comma seprated format.

This works fine for small logs but when I have started to process large logs recently I have found memory usage goes out of control. I therefore wrote a quick test script (based on the storage structure used by my program) to see if how efficient associative arrays actually are:
for ( $index1=0; $index1 < 1000; $index1++ ) { for ( $index2=0; $index2 < 1000; $index2++ ) { $array{$index1."|$index2"} = "A"; } }
At the end this nearly hit 100MB of RAM before this script finished. Obviously large logs take ages to process as the machine I run on pages itself to near death. Basically the memory used by the data structure is far greater than the data actually stored.

Is there a more efficient way to store this information in memory? Or should I just accept that I will have to begin storing the material in a temporary file (something that I want to avoid due to the potential slow down involved).

Anyone got any good ideas?

In reply to limiting associative array memory usage by FreckledAvenger

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.