Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing an application using ActiveState to convert ASP pages from a generic format to a locale specific format. To do this requires that the application load in locale settings. These settings are stuffed into a hash. The problem is that these settings have dependencies on other settings. This may lead to a dependency tree that could potentially be huge. I do not want to have problems with blowing the heap out. From all this I have 2 questions. Can someone provide a little background on how Perl allocates memory? Second is there an approach or package that could be used to ensure that memory boundaries are protected.

Thanks,
TAC

Replies are listed 'Best First'.
Re: Memory allocation
by Juerd (Abbot) on Jan 02, 2002 at 21:20 UTC
    To know how perl allocates memory, uses reference counts and such, read perlguts and/or chapter 20 of Advanced Perl Programming.

    Nothing's perfect. Perl isn't either: perl doesn't free memory, it only reuses it.
    I don't know if perl's memory usage can be limited using Perl - I always use ulimit (see: bash(1)) for that.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Memory allocation
by gwhite (Friar) on Jan 02, 2002 at 21:47 UTC

    Text::Template is pretty handy to use in these instances you can use Perl logic within the template to add or include other files, so instead of a hash with all these settings you use a template file or a combination of templates and a hash.

    g_White
Re: Memory allocation
by dmmiller2k (Chaplain) on Jan 03, 2002 at 03:44 UTC

    If I understand your question correctly, you can create a complex structure (i.e., hash of hashrefs, of hashrefs, etc.) and reference a particular object (array, hash, perl object, etc.) more than one place within the structure (i.e., multiple dependencies refer to it). Perl will not become confused by this, as evidenced by the following.

    my @test = qw( a list of words ); my %test = ( keyword => 1, value => 2) my %outer = ( hash => \%test, array => \@test); $outer{another_hash}=\%test; # add same value with another key

    When this is dumped within the Perl debugger, Perl recognizes that it has already encountered the hashref and says so:

    DB<1> x \%outer 0 HASH(0xc881d4) 'another_hash' => HASH(0xc858c0) 'keyword' => 1 'value' => 2 'array' => ARRAY(0xc85950) 0 'a' 1 'list' 2 'of' 3 'words' 'hash' => HASH(0xc858c0) -> REUSED_ADDRESS DB<1>

    Since it eschews C-style pointers, Perl manages its own heap quite well, and it is *VERY* difficult to screw up. As long as the OS has memory to give, a perl process can use as much as it needs (in my humble experience). I've seen perl processes using upwards of 1Gb of memory.

    Of course, it helps if you design your structures to be as efficient as possible, as with any language.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime