in reply to Improving memory performance

Something you can also try in some cases, is to use the same data. Let's say you have a %hash or a @list you want to work with.

You might be tempted to do something along the lines of:

my @newlist = &perform_fancy_op(@list);

But this has the side effect of pushing all of the list elements into the stack (hence, potentially growing the stack segment and your memory footprint) and also allocating a new list for the result.

If you can throw the old contents of @list, perhaps you could change your code to do something like:

&perform_efficient_op(\@list);

In this form, you only pass a reference to the whole list (a scalar) and could probably operate on the old list in place, saving the memory required for the additional copies of the list going up and down your stack.

This will usually make your code faster too, specially when dealing with long lists.

Regards.

Replies are listed 'Best First'.
Re: Re: Improving memory performance
by Sihal (Pilgrim) on Oct 04, 2002 at 08:06 UTC
    I have a way ( not thouroughly tested ) to minimize memory usage for a long running script: I fork childrens to take care of the memory intensive stuff. I then kill the forked child which brings back my perl script to a little size, and enables it to sleep without consummiong ressources. The next time it has to process very large data i refork and so on.