Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Getting a memory dump

by BrowserUk (Patriarch)
on Nov 23, 2007 at 10:11 UTC ( [id://652542]=note: print w/replies, xml ) Need Help??


in reply to Getting a memory dump

What OS are you using? If you are on Win32, I may have something that might help you track down the problem.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Getting a memory dump
by awy (Acolyte) on Nov 23, 2007 at 10:16 UTC
    I'm running this on Linux. It is possible I could try running it in Win-XP but it would take quite a bit of setup.
      It is possible I could try running it in Win-XP but it would take quite a bit of setup.

      Hm. It's possible that the code could be adapted for use on Linux, but I do not have that expertise.

      The basic idea is that I have a module (Devel::MemWatch) that hooks the DB::DB() interface and pushes the current caller information (package/file/line no) onto a Thread::Queue as each line executes. It has a setable threshhold value above which it discards old information as it pushes new, effectively turning the queue into a circular buffer of the last N lines executed.

      It also starts a background thread that wakes up every setable N millseconds and queries the process memory size from the system. If the memory expands beyond a setable limit, it dumps the circular buffer to stderr; doubles the memory limit, and goes back to monitoring.

      The idea is that you get a dump of the last N lines that executed when the memory expanded beyond your preset limit. (And again every time it doubles again). Thus, you can quickly get an idea of what code was executing when the memory started to snowball.

      The (unbelievably crude but functional) Win32 code looks like this:

      Usage is:

      perl -d:MemWatch=LINES=100,THRESHHOLD=100*1024,FREQUENCY=2000 yourscri +pt.pl

      Where

      • LINES: no. of caller traces held in the circular buffer.
      • THRESHHOLD is in Kbytes. (default 100MB)
      • FREQUENCY (at which the watchthread awakes) is in milliseconds. (Default:2 seconds)

      The output looks like:

      C:\test>perl -d:MemWatch=FREQUENCY=5000 junk9.pl FREQUENCY 5000 LINES 100 THRESHHOLD 102400 Watchthread started Memsize: 103156 main junk9.pl 8 main junk9.pl 9 main junk9.pl 9 main junk9.pl 8 main junk9.pl 9 main junk9.pl 9 ...

      Maybe you can adapt it to your needs?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thanks, that looks like it could be useful. As far as I can see, the Win32-specific bit is just the guts of the getProcessMemoryInfo sub (and the sleep). That should be easy to adapt.
        This looked like a useful idea but I have been unable to get it work for me yet.

        Adopting the calls for getProcessMemoryInfo on Linux was easy enough. Something like:

        open(PID, "/proc/$$/statm"); return (split(' ', <PID>))[5] * 4;

        but I found that the overhead of handling every statement with DB::DB was far too high. I tried to adjust the script to use DB::sub and just record subroutine calls.

        I need to include something in my DB::sub function that handles the recursion caused by the calls to $Q->pending etc. but so far I have failed to come up with a mechanism that both breaks the recursion and actually keeps on executing. Any suggestions would be appreciated.

        my $handling = 0; sub DB::sub { return &$DB::sub if $handling; $handling = 1; scalar $Q->dequeue if $Q->pending > $OPTS{ LINES }; $Q->enqueue( $DB::sub ); $handling = 0; return &$DB::sub; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found