Search code examples
perlmod-perl

How to unload modules at run-time in mod_perl?


I have a mod_perl-enable Apache running native (handlers, not Apache::Registry or similar) mod_perl code. As it is known, modules could be compiled and loaded at run-time. For testing purposes, I have a dummy module:

package MyPackage;
our $data = '0123456789' x 1000000; # 10 millions characters
1;

At run-time I load it and my httpd child processes add up 10 megabytes, one by one, after multiple requests. This is fine. This way I can use use ocasionally some memory consuming code, but while it happens rarely, I would like to be able to unload the used module after it has done its work.

If you would mention to use the lexical my variables, I know that thay are destroyed when the scope is over. So, our or package variables are an important factor in this case.

Is it possible to get this at run-time? if yes, how it could pe done?


Solution

  • The core Symbol module contains a function delete_package which takes a package name and deletes all of the variables within it.

    So in your case:

    use Symbol 'delete_package';
    
    delete_package 'MyPackage';