How do I really delete an imported module?

W

William Trenker

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
 
R

Robin Becker

.....well you have to delete all references to the module before it goes
away. There are references to re in other library modules, but I suspect
that most of re is in an extension (or built in). I believe that
extension modules are not normally unloaded.

Even so, for a python module, I think it depends on the OS whether
you'll see a decrease in vmem (and perhaps the allocation/deallocation
order).
 
T

Terry Reedy

in message
....well you have to delete all references to the module before it goes
away. There are references to re in other library modules, but I suspect
that most of re is in an extension (or built
in).

re is a relatively short Python wrapper module
(see the Lib directory) that imports the current
preferred re implementation written in C -- now
_sre, previously _pcre (or something like that).

TJR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top