|Perl: the Markov chain saw|
Is $^M a leftover April Fool?by BrowserUk (Pope)
|on Aug 29, 2003 at 22:36 UTC||Need Help??|
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.
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....
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.
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!"?
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.