How do I really delete an imported module?

Discussion in 'Python' started by William Trenker, Dec 21, 2003.

  1. 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
    William Trenker, Dec 21, 2003
    #1
    1. Advertising

  2. William Trenker

    Robin Becker Guest

    .....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
    Robin Becker, Dec 21, 2003
    #2
    1. Advertising

  3. William Trenker

    Terry Reedy Guest

    "Robin Becker" <> wrote
    in message
    news:mGZIGKA10i5$...
    > ....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
    Terry Reedy, Dec 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jp Calderone
    Replies:
    2
    Views:
    337
    Terry Reedy
    Dec 22, 2003
  2. dody suria wijaya

    Get importer module from imported module

    dody suria wijaya, Feb 7, 2005, in forum: Python
    Replies:
    5
    Views:
    382
    dody suria wijaya
    Feb 8, 2005
  3. kj
    Replies:
    9
    Views:
    285
    AK Eric
    Nov 12, 2009
  4. Dun Peal
    Replies:
    10
    Views:
    434
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    885
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page