Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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?

Replies are listed 'Best First'.
Re: How to capture out of memory excpetion in perl?
by Discipulus (Monsignor) 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 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 oxone (Friar) on Apr 23, 2008 at 10:20 UTC
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://682344]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-06-25 05:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.