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

I am trying to set aside some memory for my cache (hash/array etc) in my script. I use the ulimit and then monitor used mem with Devel::Size and destroy when i am close to my limit with %hash=(). Any ideas on how to do this better? I know I can allocate memory space by using $#arr = 999 but i am not sure how much memory it consumes. Basically my goal is to figure out how to set the max memory a variable can use and how to monitor when the max has been reached.

Replies are listed 'Best First'.
Re: Memory allocation and monitoring
by kyle (Abbot) on Apr 28, 2007 at 01:03 UTC

    I think %hash=() will not actually free any memory, but undef %hash will.

    If you're trying to monitor memory used by one variable, I think Devel::Size is your best option. If you want to monitor the size of the whole perl process, I think you'll have to go to the OS for that. How that works depends on your OS.

Re: Memory allocation and monitoring
by shigetsu (Hermit) on Apr 27, 2007 at 23:18 UTC

    I think you're confusing the memory allocation model of other languages, such as C, which has either static or dynamic allocation of memory; which may be set statically at compile-time and may be extended later at run-time (in C, realloc() & friends).

    As Perl is written in C it will apply some magic automatically to data structures when operated upon to have them extended to an appropriate size.

    There are no arbitrary limits except the ones imposed by such factors as the memory available, underlying operating system, compiler et al.