in reply to Garbage Collection and undef

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

Replies are listed 'Best First'.
Re^2: Garbage Collection and undef
by eyepopslikeamosquito (Archbishop) on Oct 17, 2004 at 08:19 UTC

    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 (malloc implementation).

Re^2: Garbage Collection and undef
by tilly (Archbishop) on Oct 17, 2004 at 06:59 UTC
    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.