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

Dear Monks,
Is there a perl command to define memory taken by array, string, etc

Replies are listed 'Best First'.
Re: memory for array
by kyle (Abbot) on Mar 27, 2008 at 16:23 UTC

    If you want to know how much is currently used by some structure, use Devel::Size.

Re: memory for array
by TGI (Parson) on Mar 27, 2008 at 17:11 UTC

    Perl variables will grow as needed. Sometimes it is useful to size an array or hash to accommodate a large number of values, to prevent Perl from repeatedly having to allocate additional space.

    # presize a hash: keys(%hash) = 10_000; # size an array: my @array; $#array = 10_000;

    Sizing an array to be shorter than it currently is will truncate the array.


    TGI says moo

Re: memory for array
by starbolin (Hermit) on Mar 27, 2008 at 19:22 UTC

    As TGI says: "Perl variables will grow as needed. " There is a lot contained in that statement. Unlike C, perl is a dynamic language. It keeps track of memory allocations for you. You rarely need to worry about it. All data types, whether scalars, arrays or hashes will have their memory allocated dynamically and will grow as needed. Perl even moves the storage around as needed when the item outgrows its allocated block and need a larger contiguous block.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}