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

Hi, I searched and found info on loading modules only when needed...but I want to unload a module from memory.

I use Archive::Zip (which is cool), but I think it takes about 1.2 Megs RAM and 8 Megs of VM. I am on Win2K.

There doesn't seem to be a way to free the memory after it is used. I know I can delay the load of it by using 'require Archive::Zip' before I create a zip...

I also read that memory (at least under Unix) won't be released back to the OS...

My big problem is swapping. I also use Tk and if you watch the swap I/O (from perfmon) it is quite bad. I have lots of RAM, but the OS does it's own thing...

Is this just the way it works???

Replies are listed 'Best First'.
Re: module unload
by erikharrison (Deacon) on Mar 15, 2002 at 01:45 UTC

    It is correct that currently memory is not returned to the system - however, it can be reused in the program, such as undefing varables (or resetting the to empty) to free memory for Perl to continue to use, thus limiting the process from expanding beyond neccesity.

    As for unloading a module (accomplishing some thing similar to deleting variables), I'm going to go out on a limb and say that there isn't a way to do this. I can't find a mention while perusing the docs (but my doc perusing ability is notoriously bad. Without some explicit statement from the programmer, Perl can't know that your going to not use a module (or class) again, and I can't find mention of sucha mechanism. Additionally, there is little deep memory management in Perl (although we all wait for the less pragma) because Perl was designed (partially) to get those kinds of things out of the programmers hair.

    Cheers,
    Erik
Re: module unload
by gt8073a (Hermit) on Mar 15, 2002 at 08:24 UTC

    Depending on what you are trying to do, you can hack your way around "Module Unloading".

    ... ## save data, ignore if you are archiving something already on disk my $blah = $tmp_dir . $$ . '.tmp'; open( TMP, ">$blah" ) or die; print TMP $data; close( TMP ); ## run another archiving script unshift( @archive_args, $blah ) my $is_archived = system( $perl, $archiving_script, @archive_args ); ## read archive, copy it, move it, unlink it, etc. ...

    Like I said, it is a hack. There are several things to keep in mind, like security, data integrity, your app will "freeze" until the system call returns, and you are only freeing memory used by Archive::Zip( not memory used by the data itself ).

    I hope this helps a little.

    Will perl for money
    JJ Knitis
    (901) 756-7693
    gt8073a@industrialmusic.com