in reply to Re: Is $^M a leftover April Fool?
in thread Is $^M a leftover April Fool?

Here's a blast from the past. You're right, the description is very straight forward. It just doesn't work.

At least, on the version of perl I was using at the time, and given the near absence of documentation of the 'feature', try as hard as I could, I could see no way of utilising it.

And, noone ever came forward with any sugggestion of how I can use $^M to allow my Perl program to obtain control once the system refuses the process further memory allocation so that I might at least be able to clean up; close filehandles, put out a "Sorry!" message, prevent the system generating a core dump. You know, that sort of practical step that $^M promises to allow.

Hmm. What do you call a feature that cannot be used?


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
•Re^3: Is $^M a leftover April Fool?
by merlyn (Sage) on Jan 06, 2005 at 15:03 UTC
    I've not tried it, but I seem to recall it working this way. At the beginning of your program, set $^M to a huge pad string:
    BEGIN { $^M = "x" x 65536 }
    Then, when you're about to take an "out of memory" hit, Perl starts the normal death mechanism, but first deallocates the $^M variable, hopefully permitting all your death handlers to do their job, because there's now 64K of breathing room to allocate temps and stuff.

    At least, this is what I recall.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I think you are correct, the problem is, what would cause my death handlers to be invoked?

      What I mean is, if my program can never recieve notification of the impending disaster, how will it know to do clean-up?

      From what I could workout, stealing 64k at the top of the program, just means that it runs out of memory 64k earlier than it would have. Then it makes use of the 64k reserve transparently, and then fails when the reserve runs out.

      As I said in the original thread, the code is there and does seem to be activated, but from the perspective of my perl script, the result is (more or less) identical because it just fails with 'Out of memory' or a Dr.Watson pop-up, or a core dump depending upon how I have my system configured.

      As far as I could work out, there is no way for my script to be notified that the reserve buffer is being used, so it has no way to do anything about it?


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
        If you are in &$SIG{__DIE__}() then your reserve buffer has been activated and this is the only memory space that you can count on.

        It's not that you know that you are out of memory. It is that you know you are dying, and if you have taken these precautions, you also know you have NNN bytes available for emergency actions.

        Be well,
        rir

      So, if this is such a good idea, why doesn't Perl reserve some small amount, like 8K or 16K, for exactly this purpose? It could even do so in $^M, to reuse the mechanism. And, if you don't want it to do that, then you can set $^M to 0. If you need more, up it. $^M x= 4 if you need 32K, etc.

      Or, is there a reason this is a bad idea?

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        I would imagine because it's often the case that a small amount isn't sufficient. Also, the amount of space needed is application dependent.

        Beside that 16K maybe not being enough, it's just a waste of resources. Most programs won't go out of memory, and if they do, dying while complaining about memory shortage is good enough. You really don't want to wast 16K (or whatever amount) for every process you are running.

        The default should be what is best for most programs. And most programs won't have any code dealing with "out of memory" situation. Therefore wasting a chunck of memory is just, well, waste.

      That's my recollection as well. And I even remember using it one time when it was first introduced to be sure that I understood how it was supposed to work. Of course, I've not looked at it or even found a situation where I thought I would need it since.

      What do you mean by "death handler"? $SIG{__DIE__}? sub DESTROY()? END{} blocks? Something else?

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson