Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

(tye)Re: Determining the Memory Usage of a Perl program from within Perl

by tye (Sage)
on Feb 15, 2001 at 21:09 UTC ( #58649=note: print w/replies, xml ) Need Help??

in reply to Determining the Memory Usage of a Perl program from within Perl

First, note that there are at least two important measures of memory use: total memory size and working set size. I tend to oversimplify them slightly and think of them as "paging file use" and "RAM use", respectively. Total memory size is how many pages of virtual memory your program has been allocated. Working set size is how many pages of memory your program needs to have in RAM at once in order to run smoothly.

If your program uses more total memory than is available to it (either due to quota restrictions or just how much is available to the operating system minus how much the kernel and other processes are using), it will probably die [well, usually malloc() will fail, which will usually either cause a quick death or trigger bugs which cause a slightly slower but more colorful death; but some operating systems will let malloc() succeed but when you actually try to use the memory the OS will notice the problem and kill you (or some other process!)].

If your program needs a larger working set than is available, then it will start "paging" too much and slow down noticeably. The size of the working set has a lot to with how the memory is accessed. By making your data structures such that related items are close together in memory, you can often dramatically reduce your program's working set size. BTW, Perl plays this same trick by trying to arrange for its most-used C subroutines to be close together in memory (which is why we have the pp_hot.c file).

To get the working set size, you'll have to ask the operating system. To get the total memory size, I'd build a version of Perl with DEBUGGING_MSTATS defined and then use Devel::Peek::mstats (though the documentation for this has been moved or purged so you may have to dig for it or reverse engineer it).

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: Determining the Memory Usage of a Perl program from within Perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://58649]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-09-20 03:11 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (171 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!