http://www.perlmonks.org?node_id=51226

meonkeys has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

How do I find the memory used by a particular hash (NOT including perl itself)?

Originally posted as a Categorized Question.

  • Comment on How do I find the memory used by a particular hash (NOT including perl itself)?

Replies are listed 'Best First'.
Re: How do I find the memory used by a particular hash (NOT including perl itself)?
by QwertyD (Pilgrim) on Apr 12, 2004 at 21:36 UTC

    Use the Devel::Size module.

    For example, to determine the amount of memory in bytes used by a hash named %hash:

    use Devel::Size qw(total_size); my $memory_used = total_size(\%hash);

    If the hash contains any references to other variables, the memory those variables use will be added as well.

Re: How do I find the memory used by a particular hash (NOT including perl itself)?
by ichimunki (Priest) on Jan 12, 2001 at 21:00 UTC

    Since a whole day went by with no answer to this, I'll give you the best I can do (and hopefully spur some discussion on the topic). If I think memory is going to be a concern with an item in my program, I would build a stop point just before that object was created (something in the application that would wait for the user to hit a key). I would (assuming I'm on linuxunixposix) then run top in a second window and wait for the size of my perl process to stabilize after hitting the stop point. Then going back to the script, continue-- all the while keeping an eye on the top window to see what happens.

    This is not an exact method, but it's useful for watching for memory leaks and getting a sense for how much memory your larger structures are eating up. I've only used it to watch my scripts as I built destructors into my Tk applications, but never to actually measure the absolute size of a hash.

Re: How do I find the memory used by a particular hash (NOT including perl itself)?
by meonkeys (Chaplain) on Jan 13, 2001 at 04:07 UTC
    I'm having trouble installing it, but my coworker was able to get more fine-grain memory use information from GTop. According to him, a 2k hash takes up 4k of real shared memory. I'll report back if I can get this to work.
Re: How do I find the memory used by a particular hash (NOT including perl itself)?
by crazyinsomniac (Prior) on Jan 15, 2001 at 03:33 UTC
    The real size of a hash is os dependant right?
    Is there a place like perlguts that talks about how hashes are stored on each os?
    If so would ut be possible to calculate or 'estimate' the real size of a hash? Like 2k would be for the pointer and 'index' and x times the length of an $hash{'element'}.

    If anyone knows these details please help out.