http://www.perlmonks.org?node_id=803539


in reply to Re: Mini-Tutorial: Perl's Memory Management
in thread Mini-Tutorial: Perl's Memory Management

The fact that a variable (or rather the allocated structure) is not freed, but rather cleared and placed in some list of preallocated structures, is an implementation detail.

I said as much. ("This is an optimisation that should be transparent to you.")

Misconceptions about the implementation details come up all the time. You've provided me with the perfect example in this paragraph. You believe it describes Perl when it doesn't.

Where'd you get a list of preallocated structures from? Placing blocks of memory on a list for future use is how freeing a variable usually works. This is exactly what Perl doesn't do.

I don't understand what are you trying to say in the "undef does not free a variable" section.

The quote line contradicts what seems to be a popular belief. The section describes what undef actually does (frees the buffers associated with the variable).

Replies are listed 'Best First'.
Re^3: Mini-Tutorial: Perl's Memory Management
by Jenda (Abbot) on Oct 28, 2009 at 10:06 UTC

    "should" sounds a bit dangerous to me. "Should be transparent" ... or so it's not always? It can fail? Or what? How? The "should" seems to cause uncertainty.

    Mkay, let's see. If we declare the variable in a loop, we are about to do another iteration and there are no other references to the data, perl may clear the contents, but leave the structure (can't remember the name) and it doesn't have to allocate memory for that variable for the next iteration. Nice. Efficient. Now what does it do if it's not in any loop? Or it was the last iteration? What if it's a my variable inside a string eval""?

    I believe your tutorial will only make sense if you first explain at least a bit about how are the data stored (the variable name points to a SV which points to the string/contains the number/whatever) and then you can go on explaining what's "cleared" or "freed" and what does the Devel::Peek::Dump mean.

    Anyway for almost everyone the whole memory allocation tutorial should be "Declare variables with my in the tightest scope possible. Make sure you either do not create circular references, use weaken() to make sure at least one link in each circle is weak or break the circle by reseting at least one reference to undef. Do not expect perl to return the memory to the OS, but that doesn't mean the memory is not freed and will not be reused, it's just kept in perl's own hands. The rest is not your business."

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re^3: Mini-Tutorial: Perl's Memory Management
by Anonymous Monk on Oct 28, 2009 at 02:17 UTC
    I said as much. ("This is an optimisation that should be transparent to you.")

    That is very obtuse way

      I think it was much less obtuse, it says a lot more and it's much clearer than "it's an implementation detail except when it matters".
        Not very much