If I look in perlvar it says:

$^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 emergency memory pool after die()ing. Suppose that your Perl were compiled with - DPERL_EMERGENCY_SBRK and used 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 to enable this option. To discourage casual use of this advanced feature, there is no English long name for this variable.

Okay, of to look in INSTALL.... Nada! Not a dicky bird!

Soooo....grep the entire source tree.

changes5.004: Title: "Document $^M in perlvar" emacs\cperl-mode.el:$^M A buffer for emergency memory allocation w +hen running out of memory. lib\text\balanced\t\extvar.t:$^M; malloc.c: # Enable code for an emergency memory pool in $^M. See p +erlvar.pod malloc.c: # for a description of $^M. malloc.c: # PERL_EMERGENCY_SBRK is enabled) without touching $^M. +See malloc.c: # perlvar.pod for a description of $^M. malloc.c: PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n" +); malloc.c: PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M! +\n"); pod\perl5004delta.pod:=item $^M pod\perl5004delta.pod:compiled for this, Perl may use the contents of +C<$^M> as an emergency pod\perl5004delta.pod: $^M = 'a' x (1<<16); pod\perl5004delta.pod:variable C<$^M>. See L<"$^M">. pod\perl5004delta.pod:However, if compiled for this, Perl may use the +contents of C<$^M> as pod\perldiag.pod:However, if compiled for this, Perl may use the conte +nts of C<$^M> as an pod\perltoc.pod:$SYSTEM_FD_MAX, $^F, $^H, %^H, $INPLACE_EDIT, $^I, $^M +, $OSNAME, $^O, pod\perltoc.pod:$^E, $^H, $^M pod\perlvar.pod:=item $^M pod\perlvar.pod:However, if suitably built, Perl can use the contents +of C<$^M> pod\perlvar.pod: $^M = 'a' x (1 << 16); win32\html\pod\perl5004delta.html:<dt><strong><a name="item__24_5em">$ +^M</a></strong><br /> win32\html\pod\perl5004delta.html:compiled for this, Perl may use the +contents of <a href="#item__24_5em"><pre>$^M</pre></a> as an emergenc +y win32\html\pod\perl5004delta.html: $^M = 'a' x (1&lt;&lt;16);</pre> win32\html\pod\perl5004delta.html:variable <a href="#item__24_5em"><pr +e>$^M</pre></a>. See <a href="#__m">$^M</a>. win32\html\pod\perl5004delta.html:However, if compiled for this, Perl +may use the contents of <a href="#item__24_5em"><pre>$^M</pre></a> as win32\html\pod\perldiag.html:However, if compiled for this, Perl may u +se the contents of <a href="file://e|\perl5.8.1-rc4\html/pod/perlvar. +html#item__24_5em"><pre>$^M</pre></a> as an win32\html\pod\perltoc.html:$SYSTEM_FD_MAX, $^F, $^H, %^H, $INPLACE_ED +IT, $^I, $^M, $OSNAME, $^O, win32\html\pod\perltoc.html:$^E, $^H, $^M win32\html\pod\perlvar.html:<dt><strong><a name="item__24_5em">$^M</a> +</strong><br /> win32\html\pod\perlvar.html:However, if suitably built, Perl can use t +he contents of <a href="#item__24_5em"><pre>$^M</pre></a> win32\html\pod\perlvar.html: $^M = 'a' x (1 &lt;&lt; 16);</pre>

Hmm. Essential several repeitions of the same passage from perlvar... ponder... what other keywords... PERL_EMERGENCY_SBRK. Try that and you get exactly the same hits as for $^M.

Then I thought about it a little.

If I assign $^M = chr(0) x 2**20; what does that do? From the (sparse) documentation it suggests that it grabs a meg of ram and holds it in reserve... then, if at somepoint during my program something I do causes perl to try and allocate some memory and it fails, it has a 1 MB 'reserve' that will allow it to continue....

Hang on!

If I grab 1 MB up front, to give perl a reserve to draw on, doesn't that just mean that malloc will fail 1 MB earlier than it would have?

So great, now perl knows it's running out of memory and has switched to the reserve tank.... but unless I--or rather my program does something different--that just means that it will carry on doing what it's doing and run out anyway! And without some way for my (program) knowing that perl has switched to the reserve tank, I'm gonna carry on tanking down the highway to oblivion.

Hmmm. Maybe there is someway to 'obtain' that notification... I could wrap everything that causes memory allocation into an eval and watch for the infamous "out of memory" error? Gonna make the program kind of messy, not to mention the problems with scoping.

Maybe I could tie $^M and trap attempts to access it? Will such magic be invoked if it is perl that is accessing it? Seems unlikely.

April Fool!

Is there a way to use $^M to good effect, or is the use of the word 'advanced' in

To discourage casual use of this advanced feature, there is no English long name for this variable.

a euphemism for "April Fool!"?


An aside

As an aside, recently, when I needed to test huge file support, I discovered that I simply didn't have enough free space on any given partition on my drive to accomodate a file of > 4 GB, which was a pain. Then inspiration struck. My filesystem supports compression. If a file consists of all the same char, it should compress pretty well, so I used a perl one-liner to write a 10 GB file that consisted of all nulls (chr(0)), and sure enough it worked. I have a file that perl reports the size of as 10 GB but that only consume 5 MB of diskspace. Perfect for testing.

That made me think about my swap file. If I create my swap file on a compressed partition, and then use perl to allocate say 100 huge scalars my $buf1 = chr(0) x 100 * 2**20;, then once each scalar gets swapped out, it would be compressed pretty small.

Now I have a program running with 10 GB of memory, which ain't bad for a 32-bit box. All I need now is a way to make use of all that RAM:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Is $^M a leftover April Fool? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.