How do I really delete an imported module?

Jp Calderone exarkun at intarweb.us
Mon Dec 22 01:50:28 CET 2003


On Sun, Dec 21, 2003 at 02:24:07PM +0000, William Trenker wrote:
> I've been googling around trying to figure out how to delete an imported module and free up the memory it was using.  One point of confusion, for me, is that if I "del" a module the virtual memory used by the process doesn't decrease.  Using Python 2.3.2 on Linux, here is a simple test program that prints out the virtual memory size and the relative contents of the sys.modules dict at various points:
> 
> import sys,os,pprint,sets
> def vmem(text):
>     print '%s: VmSize=%skB'%(text,os.popen('ps h o vsize %s'%os.getpid()).read().strip())
> def mods():
>     pprint.pprint(sets.Set(sys.modules.keys()) - baseModules)
> baseModules = sets.Set(sys.modules.keys())
> vmem('begin')
> import re
> vmem('after import re')
> mods()
> del re
> vmem('after del re')
> mods()
> 
> Here is the output:
> 
> begin: VmSize=3348kB
> after import re: VmSize=3408kB
> Set(['strop', 'sre', '_sre', 'sre_constants', 're', 'sre_compile', 'sre_parse', 'string'])
> after del re: VmSize=3408kB
> Set(['strop', 'sre', '_sre', 'sre_constants', 're', 'sre_compile', 'sre_parse', 'string'])
> 
> The sys.modules items related to the "re" library module are still showing up after "del re".  And the virtual memory for the process has not been reduced.

  One important thing to realize is that "import re" does not just create
one reference to the "re" module.  It also creates a reference at
sys.modules['re'], and possibly in many other places as well.  Each of the
modules listed in the output above also is referenced from sys.modules, so
there is no possibility of them being garbage collected until that reference
is removed.

> 
> What is the pythonic way to completely delete a module and get it's memory garbage collected?
> 

  Another important thing to realize is that some modules may be implemented
as extensions.  The _sre module is one such module.  This means that to
import it, Python first calls dlopen() (or whatever the platform's
equivalent is) on the file containing the object code.  While it could later
call dlclose() if it knows the module is no longer referenced, it does not. 
Even if it did, there is no guarantee that the underlying platform would
then release the memory associated with it (Most platforms do, but not all). 
Python has no control over this.

  Jp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/python-list/attachments/20031221/a345c963/attachment.pgp 


More information about the Python-list mailing list