Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I really delete an imported module?

Reply
Thread Tools

How do I really delete an imported module?

 
 
William Trenker
Guest
Posts: n/a
 
      12-21-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

 
Reply With Quote
 
 
 
 
Robin Becker
Guest
Posts: n/a
 
      12-21-2003
.....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).
--
Robin Becker
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      12-22-2003

"Robin Becker" <(E-Mail Removed)> wrote
in message
news:mGZIGKA10i5$(E-Mail Removed)...
> ....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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why do directly imported variables behave differently than thoseattached to imported module? Dun Peal Python 10 05-03-2011 10:11 PM
Delete does not really delete Hong Chen C++ 2 06-16-2008 08:49 PM
"Variable ... is not imported..." using an imported variable from a module Volker Nicolai Perl Misc 9 07-04-2005 08:34 AM
Re: How do I really delete an imported module? Jp Calderone Python 2 12-22-2003 05:07 PM



Advertisments