Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

( #51226=categorized question: print w/ replies, xml ) Need Help??
Contributed by meonkeys on Jan 12, 2001 at 02:09 UTC
Q&A  > programs and processes


Description:

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

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

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.

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

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.

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

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.

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

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (10)
    As of 2014-04-16 10:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (422 votes), past polls