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

Hi all,

I would like to know whether it's possible to remove an unused module from memory, once it's not used anymore. I have figured out how to load it at the very last moment using require, but how could I unload it ?

Thanks for your help.

Replies are listed 'Best First'.
Re: Loading/Unloading perl modules
by busunsl (Vicar) on Sep 27, 2001 at 14:13 UTC
    no module;
    removes the imports of the module. I don't know if it frees memory too.
    Have a look at perldoc -f use.
      I gave a try at that...

      It works fine for standard pragmas, but requires an unimport function to exist in the package to unload. That does not work for XML::DOM, or even Shell...
Re (tilly) 1: Loading/Unloading perl modules
by tilly (Archbishop) on Sep 27, 2001 at 16:41 UTC
    Sorry, you are probably out of luck.

    Once code is compiled, it stays around in memory. If the module built up large data structures, you could try to find a way to destroy them. But even if your program is no longer using the memory, it is unlikely that your operating system will be able to take the memory back before you expire.

    If you really, really need to do this, then the simplest approach is to launch another process (or fork), have that one load the module, do what needs to be done, then exit. Since the memory was tied up in a process that exited, it is all freed at once.