As h0mee and I were playing pool last night, the topic came up of perl's garbage collection system -- of which, I asked him this question:*

Why does perl's GC system not re-allocate the memory that a given lexically scoped variable consumes when said variable goes out of scope?

Pondering Automagical De-allocation of Memory:

We decided that it would be prohibitively difficult and possibly pretty dangerous (as far as perl having to allocate memory to the same variable another time, after re-claiming the memory), because perl would have to guarantee that the variable would never be mentioned again, much like the time redmist caused the "incident" at Thanksgiving.

I thought about this, and then thought of what I had read on page 56 of Camel 3, earlier in the day about name lookups. A name lookup occurs whenever you declare a variable with my or our. (So sayeth Camel 3...if a name lookup occurs any other time, lemme know.) The five rules regarding name lookups on that page (basically) say that when a variable is declared within a scope, perl looks in a bunch of places (outside the block (if any -- and outside the second block, if any, and so forth). After it is done looking through the code blocks, perl takes a look at the compilation unit. Whether this is an eval()**, or the file itself (i.e. not in any block at all (unless you consider the file itself a block)). If a variable is declared twice, an error is raised.

And so+, perl tries to find another variable of the same name. Why couldn't perl use a similar process to decide whether there was any name-dropping of the variable in question outside the variable's scope, and/or any process that would involve coming back to said scope? There is one problem that my mal-developed eyes can see:

This idea would most likely necessitate some pretty major changes in perl's GC system (that was most likely a very large understatement). Perl internals gurus: feel free to comment.

Why Not Give The Programmer a Little freedom?

Then, we thought, the programmer would probably have a better idea of which variables he wanted to throw away. Why not provide perl with a builtin free function? Some would argue that it would make the feet much more shootable. Although h0mee and I disagree with that argument, and propose that there are lots of ways to shoot yourself in the foot. There are two ways that I know of to make the feet more bullet resistant:

The pragma could be called risk, which would allow free to be used. The programmer could de-allocate memory if (s)he was certain that the corresponding variable would never be mentioned again. If perl removed the variable from the symbol table, and by some code-related fumble of grandiose proportions, the variable was named again, it would raise an error under the strict pragma.

But enough of this hypothetical mumbo-jumbo! Who cares? Well, I was hoping to spawn some interesting conversation about Perl internals.++ Kind of a round about way to do it, but what the hell? Who knows, maybe some good will come of it.

So is this possible? Is it wise? What do you think? </p * Yes, I do mean perl. Follow this link for an explanation.
** With eval() EXPR, the "edges" of the *contents* of EXPR act as curlies.
+ There are some more actions that perl takes to find another variable of the same name, but they are not germane to our current exchange...although there is no guarantee that the preceding information above was relavant either.
++ And maybe how much I suck at understanding perl's guts.


In reply to free 0x010400! by redmist

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.