How do I really delete an imported module?

William Trenker wtrenker at shaw.ca
Sun Dec 21 09:24:07 EST 2003


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.

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

Thanks,
Bill





More information about the Python-list mailing list