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

I am writing a program that will need to maintain 2 linked lists, one where each node will contain roughly 40bytes of data each, and the other where the nodes will contain 338bytes.

The list with the smaller nodes will probably have as many as 1,000,000 nodes, the one with the larger node sizes will have about 60,000 nodes, That comes to 40bm + 20mb, 60mb, it is possible for each of these lists to double or half in size, the size of the lists are my predictions.

so I can be using anywhere from 30mb to 120mb for these 2 lists, not counting the actual nodes themselves..

I have 1gb of ram, plus 4gb of swap. I know my system has the memory to handle this, but does perl have problems with this much memory usage?

Replies are listed 'Best First'.
Re: perl memory use question
by Joost (Canon) on Jun 03, 2007 at 20:32 UTC
    My test says it takes about 200Mb for a single-linked list.

    If you don't need linked lists, using plain arrays is probably more memory efficient.

    #!perl/bin/perl use strict; $|=1; my ($list1,$list2); print "Making list1\n"; for (0 .. 1_000_000) { my $head = $list1; $list1 = ["a" x 40,\$head]; } print "Making list2\n"; for (0 .. 60_000) { my $head = $list2; $list2 = ["b" x 338,\$head]; } print "Press enter to exit: "; my $dummy = <STDIN>;
    update: perl should be fine with that kind of usage. just remember that perl is no where near as memory-efficient as C can be. perl values have a significant overhead. especially if you have many small objects.

    update 2: i get a segfault from this script at destruction. that's not right. (tested perl 5.00504 & perl 5.8.8)

    update 3: ok, increasing the stack size limit (ulimit -s 400000) fixes the problem.

      Joost,

      For fun, I tried running your program on ASP (version 5.8.8, Binary build 819), and got a software exception.  That's not to say it's anything with your program of course, I'm sure it's just my memory configuration.

      I narrowed it down to:

      #!perl/bin/perl use strict; use warnings; $|=1; my ($list1); print "Making list1\n"; for (0 .. 93132) { my $head = $list1; $list1 = ["a" x 40,\$head]; }

      which does give the error.  When 93132 is changed to 93131, the error does NOT occur.

      This is with Windows XP, and 512 MB of RAM.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      how do I use ulimit/ set the stack size? is it a flag when I run the script, or is it a line in the script?
Re: perl memory use question
by BrowserUk (Patriarch) on Jun 04, 2007 at 00:05 UTC

    If you put your data items in to a normal array, and form the linked list by prefixing the index of the next item to the front (or stick it on the end), then your worst case of 2e6 x 40b and 120e3 x 338, only requires 1/4GB.

    Hide the mechanics behind a suitable OO interface and you can hand out instance handles that are just blessed integers.

    #! perl -slw use strict; our $BIG ||= 60e3; our $LIL ||= 1e6; my( @biguns, @liluns ); push @biguns, pack 'NA338', scalar @biguns, 'X' x 338 for 1 .. $BIG; push @liluns, pack 'NA40', scalar @liluns, 'Y' x 40 for 1 .. $LIL; system qq[tasklist /fi "pid eq $$"]; <STDIN>; __END__ C:\test>618996 -BIG=60e3 -LIL=1e6 Image Name PID Session Name Session# Mem Usag +e ========================= ====== ================ ======== =========== += perl.exe 2340 0 123,984 +K C:\test>618996 -BIG=120e3 -LIL=2e6 Image Name PID Session Name Session# Mem Usag +e ========================= ====== ================ ======== =========== += perl.exe 2656 0 246,096 +K

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: perl memory use question
by chromatic (Archbishop) on Jun 03, 2007 at 23:01 UTC
    I know my system has the memory to handle this, but does perl have problems with this much memory usage?

    Not if your system has the memory to handle it.

Re: perl memory use question
by varian (Chaplain) on Jun 04, 2007 at 09:25 UTC
    The list with the smaller nodes will probably have as many as 1,000,000 nodes
    Even if you can maintain this in memory in Perl you may want to reconsider if you want to maintain this in memory.

    For large amounts of data it usually makes sense to temporarily store them on disk or other media and to process them in chunks. It draws much less on system resources and it creates a more scalable solution.
    For an example of this type of solutions see the famous mergesort algorithm that can sort an unlimited amount of data as it is not limited by available system memory.