Okay. This is just a skeleton, but this creates 50,000 Bus objects, and gives each of them 33 x 80-byte timetables. All are individually getable and setable. All fully OO (externally).

Total data: 50,000 * 33 * 80 = 125 MB.

Total process memory consumed: 140 MB.

Adding methods to manipulate the data is just a case of each method calling the get() routine and then splitting the data into it's constituent bits to manipulate. Trading a little time for memory.

#! perl -slw use strict; package Buses; sub new { my( $class, $timeTablesObj ) = @_; return bless \$timeTablesObj, $class; } sub getTimeTable { my( $self, $id ) = @_; return $$self->get( $id ); } sub setTimeTable { my( $self, $id, $new ) = @_; return $$self->set( $id, $new ); } package Timetables; sub new{ my( $class, $timetable ) = @_; return bless \$timetable, $class; } sub get{ my( $self, $id ) = @_; ## Numeric ID, zero based return substr $$self, $id*80, 80; } sub set{ my( $self, $id, $new ) = @_; substr $$self, $id*80, 80, pack 'a80', $new; } package main; my @buses = map { my $timeTable = Timetables->new( join '', map{ pack 'A80', $_ } 0 .. 33 ); Buses->new( $timeTable ); } 1 .. 50_000; print $buses[ 49_000 ]->getTimeTable( 29 ); <STDIN>;

Or if you need text-key access to your buses using a hash pushes it to 150 MB.

#! perl -slw use strict; package Buses; sub new { my( $class, $timeTablesObj ) = @_; return bless \$timeTablesObj, $class; } sub getTimeTable { my( $self, $id ) = @_; return $$self->get( $id ); } sub setTimeTable { my( $self, $id, $new ) = @_; return $$self->set( $id, $new ); } package Timetables; sub new{ my( $class, $timetable ) = @_; return bless \$timetable, $class; } sub get{ my( $self, $id ) = @_; ## Numeric ID, zero based return substr $$self, $id*80, 80; } sub set{ my( $self, $id, $new ) = @_; substr $$self, $id*80, 80, pack 'a80', $new; } package main; my %buses = map { my $timeTable = Timetables->new( join '', map{ pack 'A80', $_ } 0 .. 33 ); ( "BusID:$_", Buses->new( $timeTable ) ); } 1 .. 50_000; print $buses{ "BusID:49001" }->getTimeTable( 29 ); <STDIN>;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re: Reducing Memory Usage ( under 10%) by BrowserUk
in thread Reducing Memory Usage by PerlingTheUK

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.