Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Garbage Collection and undef

by Irinotecan (Novice)
on Oct 17, 2004 at 02:22 UTC ( #399862=perlquestion: print w/replies, xml ) Need Help??

Irinotecan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Perl neophyte here. I'm wondering how to best go about recovering memory (internal to Perl, of course) for a large array.
my @array = (a, large, array, etc....); ... #Later, should I do: undef @array; #or @array = (); #And, if I want to re-use the variable even later, if I use undef, do +I: my @array = (a, new, array);
I guess what my real question is, does undef just force garbage collection, or does it actually un-declare the my definition, so that something later like @array=(...); creates a global? Thanks for any insight.

Replies are listed 'Best First'.
Re: Garbage Collection and undef
by davido (Cardinal) on Oct 17, 2004 at 04:11 UTC

    If you are using lexical variables with narrow scopes, @array might just fall out of scope and be garbage collected without you undeffing it. However, if you really do need to wipe it out and allow its memory to be reclaimed by Perl, undef @array or @array = () will suffice.

    But maybe tell us this: what are you trying to do? It's unusual to really have to think about Perl's garbage collection, unless you're worried about reference counts or something. But for the most part, if you're designing your scoping well, there's not usually a need to worry about clearing variables.

    A little more detail on what you're dealing with and trying to accomplish might enable us to help steer you toward a solution where you don't need to micromanage the garbage collection.


    Dave

Re: Garbage Collection and undef
by ikegami (Pope) on Oct 17, 2004 at 06:24 UTC
    While undef @array frees up the memory, it only frees it up for perl to use, not for the OS. At least, that's what I remember from long ago in mod_perl discussions. Can someone confirm this?

    Update: In Windows: (Numbers from Task Manager)

    print("Press Enter to allocate a big chunk of memory."); <STDIN>; @a = (0..100000); print("Memory allocated.\n"); print("Press Enter to free it."); <STDIN>; undef @a; print("Memory freed.\n"); print("Press Enter to exit perl."); <STDIN>; __END__ 6,328K initially 8,332K after allocating 7,952K after freeing.
    print("Press Enter to allocate a big chunk of memory."); <STDIN>; { my @a = (0..100000); $a[0] = $a[0]; print("Memory allocated.\n"); print("Press Enter to free it."); <STDIN>; } print("Memory freed.\n"); print("Press Enter to exit perl."); <STDIN>; __END__ 6,328K initially 6,728K after allocating 6,728K after freeing

      Whether the memory is returned to OS or not depends simply on the malloc/realloc/free functions perl was built with. It is possible to get perl to release memory to the OS (with some effort) by building a custom perl with custom malloc et al. as discussed in more detail in Re: Not able to release memory.

      I can sort of confirm it, and sort of deny it.

      It is true that at one point Perl would never return memory to the OS. However someone fairly prominent in p5p (I think that it was chip) told me a few years ago that he had seen a program that did return memory to the OS, had looked into how the trick was done, and taught Perl to do it with some operating systems. So while you cannot rely on memory being returned to the OS, on some operating systems under some circumstances, Perl might do so.

Re: Garbage Collection and undef
by osunderdog (Deacon) on Oct 17, 2004 at 03:34 UTC

    There are some comments on Memory.

    No you don't redeclare the variable. IE my. Unless it's in a different scope.

    also I think undef and assigning the array to an empty array as the same operation. (Someone will probably correct me if I'm wrong.)

    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog
      I think undef and assigning the array to an empty array as the same operation. (Someone will probably correct me if I'm wrong.)

      You're wrong :-) undefing the array will free the memory associated with the array back to the arena to be reused later. @array = (); does not free the memory used by the array. This has the primary advantage of saving allocation later if you're continually putting stuff in the array then clearing it. (i.e. perl doesn't need to regrow the array each time you put stuff in it after it has been cleared)

      But ... both of them make the array empty, so if that's all you meant by "the same operation", then you're okay. ;-)

Re: Garbage Collection and undef
by pg (Canon) on Oct 17, 2004 at 04:12 UTC
    "does undef just force garbage collection, or does it actually un-declare the my definition"

    If it undeclares the array, then the following would have syntax error, but it actually not, so the answer is clear:

    use strict; use warnings; my @a = (1,2,3); undef @a; $a[1] = 2; @a = undef; $a[1] = 2;

    Also garbage collector does not free memory right after you tell it to, it kicks in according to its own schedule.

      Also garbage collector does not free memory right after you tell it to, it kicks in according to its own schedule.

      But Perl 5 uses reference counting, not true garbage collection, so it does free memory immediately.

        It is a misunderstaning that reference counting always causes immediate garbage collection. There are different algorithms, and counting reference is much more complex than you thought. As a matter fact, in order to resolve problems including cyclic garbage, Perl uses Deutsch and Bobrow's deferred reference counting. This algorithm ignores updates to local variables, and it periodically scans the stack to determine the true reference counts.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://399862]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2019-11-22 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (110 votes). Check out past polls.

    Notices?