Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to capture out of memory excpetion in perl?

by mihirjha (Novice)
on Apr 23, 2008 at 09:09 UTC ( #682344=perlquestion: print w/ replies, xml ) Need Help??
mihirjha has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!, How can we capture out of memory exception in perl ? Regards, Mihir

Comment on How to capture out of memory excpetion in perl?
Re: How to capture out of memory excpetion in perl?
by mscharrer (Hermit) on Apr 23, 2008 at 09:20 UTC
    I don't know any way in Perl to do this directly and I wouldn't be surprised if there isn't one.

    One idea would be to write a Perl script which is calling a other Perl script using system( ) inside eval and test if it's returns an error. However this wouldn't be very clean and portable. Untested.

Re: How to capture out of memory excpetion in perl?
by haoess (Curate) on Apr 23, 2008 at 09:26 UTC

    perldiag says:

    Out of memory! (X) The malloc() function returned 0, indicating there was insufficien +t remaining memory (or virtual memory) to satisfy the request. Perl h +as no option but to exit immediately. At least in Unix you may be able to get past this by increasing your p +rocess datasize limits: in csh/tcsh use limit and limit datasize n (w +here n is the number of kilobytes) to check the current limits and ch +ange them, and in ksh/bash/zsh use ulimit -a and ulimit -d n , respec +tively.

    But maybe you can fork the process out and capture the output/exit code.

    -- Frank

Re: How to capture out of memory excpetion in perl?
by Discipulus (Curate) on Apr 23, 2008 at 09:47 UTC
    I cannot find an old discussion about the misterious $^M special variable but I found this:
    $^M By default, running out of memory is an untrappable, fatal error. However, if suitably built, Perl can use the contents of $^M as an eme +rgency memory pool after die()ing. Suppose that your Perl were compiled with -DPERL_EMERGENCY_SBRK and u +sed Perl's malloc. Then $^M = 'a' x (1 << 16); would allocate a 64K buffer for use in an emergency. See the INSTALL file in the Perl distribution for information on how t +o add custom C compilation flags when compiling perl. To discourage casual use of this advanced feature, there is no English + long name for this variable.
    ah ok is this: 287850

    Hope some sage can tell how to use this!!
    HTH Lor*
      In my experience with similar things, the idea is that your code gets called (via signal or callback) when the program dips into the emergency pool ($^M). It should then immediately rummage around and try to free stuff, then try to reallocate the emergency pool. If it fails, then you're probably doomed.

      Unfortunately, I don't see a relevant callback for $^M. If so, then there's no way to tell when $^M has been used, and I don't see how it can be useful.

Re: How to capture out of memory excpetion in perl?
by oxone (Friar) on Apr 23, 2008 at 10:20 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://682344]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-27 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls