newbie question - remove a module from ram

Discussion in 'Python' started by john fabiani, May 10, 2004.

  1. john fabiani

    john fabiani Guest

    Hi,

    I believe I have good understanding of import but it occurred to me that
    I might want to remove an imported module. I.e I load a module into ram
    and I no longer need the module. Modules just stays in ram? In the
    windows world I would "thisform.release()" and the garbage collector
    would release the ram. So did I miss something or is there no release
    method. How about a method within a class like destroy()?

    I just got to believe it's there???? But where?
    John
     
    john fabiani, May 10, 2004
    #1
    1. Advertising

  2. john fabiani

    Paul McGuire Guest

    "john fabiani" <> wrote in message
    news:4AAnc.6791$...
    > Hi,
    >
    > I believe I have good understanding of import but it occurred to me that
    > I might want to remove an imported module. I.e I load a module into ram
    > and I no longer need the module. Modules just stays in ram? In the
    > windows world I would "thisform.release()" and the garbage collector
    > would release the ram. So did I miss something or is there no release
    > method. How about a method within a class like destroy()?
    >
    > I just got to believe it's there???? But where?
    > John


    Well, you were pretty close with calling something like .release(). Use the
    del statement.

    >>> import random
    >>> print random.random()

    0.475899061786
    >>> del random
    >>> print random.random()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'random' is not defined
    >>>


    -- Paul
     
    Paul McGuire, May 10, 2004
    #2
    1. Advertising

  3. john fabiani

    Peter Otten Guest

    Paul McGuire wrote:

    > "john fabiani" <> wrote in message
    > news:4AAnc.6791$...
    >> Hi,
    >>
    >> I believe I have good understanding of import but it occurred to me that
    >> I might want to remove an imported module. I.e I load a module into ram
    >> and I no longer need the module. Modules just stays in ram? In the
    >> windows world I would "thisform.release()" and the garbage collector
    >> would release the ram. So did I miss something or is there no release
    >> method. How about a method within a class like destroy()?
    >>
    >> I just got to believe it's there???? But where?
    >> John

    >
    > Well, you were pretty close with calling something like .release(). Use
    > the del statement.
    >
    >>>> import random
    >>>> print random.random()

    > 0.475899061786
    >>>> del random
    >>>> print random.random()

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'random' is not defined
    >>>>


    and then

    >>> import sys
    >>> sys.modules["random"].random()

    0.43459738002826365
    >>>


    should make it clear that (next to) no memory is freed in the process.

    Peter
     
    Peter Otten, May 10, 2004
    #3
  4. john fabiani

    Paul McGuire Guest

    "Peter Otten" <> wrote in message
    news:c7nvr1$fsc$01$-online.com...
    > Paul McGuire wrote:
    >
    > > "john fabiani" <> wrote in message
    > > news:4AAnc.6791$...
    > >> Hi,
    > >>
    > >> I believe I have good understanding of import but it occurred to me

    that
    > >> I might want to remove an imported module. I.e I load a module into

    ram
    > >> and I no longer need the module. Modules just stays in ram? In the
    > >> windows world I would "thisform.release()" and the garbage collector
    > >> would release the ram. So did I miss something or is there no release
    > >> method. How about a method within a class like destroy()?
    > >>
    > >> I just got to believe it's there???? But where?
    > >> John

    > >
    > > Well, you were pretty close with calling something like .release(). Use
    > > the del statement.
    > >
    > >>>> import random
    > >>>> print random.random()

    > > 0.475899061786
    > >>>> del random
    > >>>> print random.random()

    > > Traceback (most recent call last):
    > > File "<stdin>", line 1, in ?
    > > NameError: name 'random' is not defined
    > >>>>

    >
    > and then
    >
    > >>> import sys
    > >>> sys.modules["random"].random()

    > 0.43459738002826365
    > >>>

    >
    > should make it clear that (next to) no memory is freed in the process.
    >
    > Peter
    >

    So then what if he follows up with:

    del sys.modules["random"]

    Are there any other dangling references to this module that would stymie the
    garbage collector (assuming that the OP hasn't saved off his own reference
    to the module)?

    -- Paul
     
    Paul McGuire, May 10, 2004
    #4
  5. john fabiani

    Peter Otten Guest

    Paul McGuire wrote:

    > "Peter Otten" <> wrote in message
    > news:c7nvr1$fsc$01$-online.com...
    >> Paul McGuire wrote:
    >>
    >> > "john fabiani" <> wrote in message
    >> > news:4AAnc.6791$...
    >> >> Hi,
    >> >>
    >> >> I believe I have good understanding of import but it occurred to me

    > that
    >> >> I might want to remove an imported module. I.e I load a module into

    > ram
    >> >> and I no longer need the module. Modules just stays in ram? In the
    >> >> windows world I would "thisform.release()" and the garbage collector
    >> >> would release the ram. So did I miss something or is there no release
    >> >> method. How about a method within a class like destroy()?
    >> >>
    >> >> I just got to believe it's there???? But where?
    >> >> John
    >> >
    >> > Well, you were pretty close with calling something like .release().
    >> > Use the del statement.
    >> >
    >> >>>> import random
    >> >>>> print random.random()
    >> > 0.475899061786
    >> >>>> del random
    >> >>>> print random.random()
    >> > Traceback (most recent call last):
    >> > File "<stdin>", line 1, in ?
    >> > NameError: name 'random' is not defined
    >> >>>>

    >>
    >> and then
    >>
    >> >>> import sys
    >> >>> sys.modules["random"].random()

    >> 0.43459738002826365
    >> >>>

    >>
    >> should make it clear that (next to) no memory is freed in the process.
    >>
    >> Peter
    >>

    > So then what if he follows up with:
    >
    > del sys.modules["random"]
    >
    > Are there any other dangling references to this module that would stymie
    > the garbage collector (assuming that the OP hasn't saved off his own
    > reference to the module)?


    Python 2.3.3 (#1, Jan 3 2004, 13:57:08)
    [GCC 3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import random, sys, weakref
    >>> sys.getrefcount(random)

    3
    >>> del sys.modules["random"]
    >>> sys.getrefcount(random)

    2

    This is one reference for the function call and one for the __main__.random
    reference, i. e. reference count should indeed go to zero if we perform

    del random

    I could not trick the module into telling me directly:

    >>> def f(r): print "i'm gone"

    ....
    >>> w = weakref.ref(random, f)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: cannot create weak reference to 'module' object
    >>>


    Second try, working around the limitations of weak references:

    >>> import random, sys, weakref
    >>> class Flag: pass

    ....
    >>> random.flag = Flag()
    >>> def goodbye(r): print "I'm gone"

    ....
    >>> w = weakref.ref(random.flag, goodbye)
    >>> del random
    >>> del sys.modules["random"]

    I'm gone
    >>>


    So at least the objects in the module are garbage-collected.

    Peter
     
    Peter Otten, May 10, 2004
    #5
  6. john fabiani

    john fabiani Guest

    Paul McGuire wrote:
    > "Peter Otten" <> wrote in message
    > news:c7nvr1$fsc$01$-online.com...
    >
    >>Paul McGuire wrote:
    >>
    >>
    >>>"john fabiani" <> wrote in message
    >>>news:4AAnc.6791$...
    >>>
    >>>>Hi,
    >>>>
    >>>>I believe I have good understanding of import but it occurred to me

    >
    > that
    >
    >>>>I might want to remove an imported module. I.e I load a module into

    >
    > ram
    >
    >>>>and I no longer need the module. Modules just stays in ram? In the
    >>>>windows world I would "thisform.release()" and the garbage collector
    >>>>would release the ram. So did I miss something or is there no release
    >>>>method. How about a method within a class like destroy()?
    >>>>
    >>>>I just got to believe it's there???? But where?
    >>>>John
    >>>
    >>>Well, you were pretty close with calling something like .release(). Use
    >>>the del statement.
    >>>
    >>>
    >>>>>>import random
    >>>>>>print random.random()
    >>>
    >>>0.475899061786
    >>>
    >>>>>>del random
    >>>>>>print random.random()
    >>>
    >>>Traceback (most recent call last):
    >>> File "<stdin>", line 1, in ?
    >>>NameError: name 'random' is not defined
    >>>

    >>and then
    >>
    >>
    >>>>>import sys
    >>>>>sys.modules["random"].random()

    >>
    >>0.43459738002826365
    >>
    >>should make it clear that (next to) no memory is freed in the process.
    >>
    >>Peter
    >>

    >
    > So then what if he follows up with:
    >
    > del sys.modules["random"]
    >
    > Are there any other dangling references to this module that would stymie the
    > garbage collector (assuming that the OP hasn't saved off his own reference
    > to the module)?
    >
    > -- Paul
    >
    >

    I think I follow: but doesn't the garbage collector decide what is to
    be done? Assuming that there were no dangling references wouldn't the
    ram be available for re-use?
    John
     
    john fabiani, May 10, 2004
    #6
  7. john fabiani

    Terry Reedy Guest

    "john fabiani" <> wrote in message
    news:4AAnc.6791$...
    > Hi,
    >
    > I believe I have good understanding of import but it occurred to me that
    > I might want to remove an imported module. I.e I load a module into ram
    > and I no longer need the module. Modules just stays in ram? In the
    > windows world I would "thisform.release()" and the garbage collector
    > would release the ram. So did I miss something or is there no release
    > method. How about a method within a class like destroy()?
    >
    > I just got to believe it's there???? But where?


    The delete statement, in general, directs the interpreter to delete the
    association between a name or any other slot and the previously associated
    object. Ditto for function locals at function return. It does not tell
    the interpreter what to do with a object when its last association
    disappears. If, for a particular implementation, it makes sense to talk of
    the object being in 'deletable memory' (and, for humans, it hardly does,
    nor would it for read-only machine memory, nor, possibly, for 'builtin'
    modules), then the standard *allows* but does not *require* deletion.

    CPython currently caches a reference to modules so that code like the
    following won't tear down a module as it returns and have to rebuild it at
    every call:

    def sincos(x):
    import math as m # for the purpose of this example, assume this is only
    math import
    return sin(x), cos(x)

    Modules are sufficiently small, sufficiently expensive to set up, and
    sufficiently often reused, that I has not been thought worthwhile to add a
    modwipe function to countervail the cache effect, which itself was added to
    countervail the usual CPython delete-object- with-last-reference behavior.

    Terry J. Reedy
     
    Terry Reedy, May 10, 2004
    #7
  8. john fabiani <> writes:

    > Paul McGuire wrote:
    > > So then what if he follows up with:
    > > del sys.modules["random"]
    > > Are there any other dangling references to this module that would
    > > stymie the
    > > garbage collector (assuming that the OP hasn't saved off his own reference
    > > to the module)?
    > > -- Paul
    > >

    > I think I follow: but doesn't the garbage collector decide what is to
    > be done? Assuming that there were no dangling references wouldn't
    > the ram be available for re-use?


    Well, that depends on vagaries of your allocator, but probably.

    Why are you so worried about the RAM your modules take up?

    Cheers,
    mwh

    --
    The use of COBOL cripples the mind; its teaching should, therefore,
    be regarded as a criminal offence.
    -- Edsger W. Dijkstra, SIGPLAN Notices, Volume 17, Number 5
     
    Michael Hudson, May 10, 2004
    #8
  9. john fabiani

    john fabiani Guest

    Michael Hudson wrote:

    > john fabiani <> writes:
    >
    >
    >>Paul McGuire wrote:
    >>
    >>>So then what if he follows up with:
    >>> del sys.modules["random"]
    >>>Are there any other dangling references to this module that would
    >>>stymie the
    >>>garbage collector (assuming that the OP hasn't saved off his own reference
    >>>to the module)?
    >>>-- Paul
    >>>

    >>
    >>I think I follow: but doesn't the garbage collector decide what is to
    >>be done? Assuming that there were no dangling references wouldn't
    >>the ram be available for re-use?

    >
    >
    > Well, that depends on vagaries of your allocator, but probably.
    >
    > Why are you so worried about the RAM your modules take up?
    >
    > Cheers,
    > mwh
    >

    Well I guess I don't have an example that I can bring up. It's just an
    after thought that if I create a large program that everything is being
    stored in ram (or swap). At some point I'll run out of ram and the more
    I limit the stuff I load and can't release the better. In the windows
    world I release(). When I monitor the ram usage (task manager) I can
    see the ram usage lower when I release(). I keep thinking about all the
    forms and the associated code I load in my windows programs. If I kept
    all the forms in ram I'd run out quickly. Of course I work with
    accounting packages....
    John
     
    john fabiani, May 10, 2004
    #9
  10. john fabiani

    Steve Holden Guest

    Terry Reedy wrote:

    > "john fabiani" <> wrote in message
    > news:4AAnc.6791$...
    >
    >>Hi,
    >>
    >>I believe I have good understanding of import but it occurred to me that
    >>I might want to remove an imported module. I.e I load a module into ram
    >>and I no longer need the module. Modules just stays in ram? In the
    >>windows world I would "thisform.release()" and the garbage collector
    >>would release the ram. So did I miss something or is there no release
    >>method. How about a method within a class like destroy()?
    >>
    >>I just got to believe it's there???? But where?

    >
    >
    > The delete statement, in general, directs the interpreter to delete the
    > association between a name or any other slot and the previously associated
    > object. Ditto for function locals at function return. It does not tell
    > the interpreter what to do with a object when its last association
    > disappears. If, for a particular implementation, it makes sense to talk of
    > the object being in 'deletable memory' (and, for humans, it hardly does,
    > nor would it for read-only machine memory, nor, possibly, for 'builtin'
    > modules), then the standard *allows* but does not *require* deletion.
    >
    > CPython currently caches a reference to modules so that code like the
    > following won't tear down a module as it returns and have to rebuild it at
    > every call:
    >
    > def sincos(x):
    > import math as m # for the purpose of this example, assume this is only
    > math import
    > return sin(x), cos(x)
    >
    > Modules are sufficiently small, sufficiently expensive to set up, and
    > sufficiently often reused, that I has not been thought worthwhile to add a
    > modwipe function to countervail the cache effect, which itself was added to
    > countervail the usual CPython delete-object- with-last-reference behavior.
    >
    > Terry J. Reedy
    >

    Note also that there is, AFAICT, no way to reclaim space occupied by the
    code of extension modules.

    regards
    Steve
     
    Steve Holden, May 10, 2004
    #10
    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. Robert Posey
    Replies:
    0
    Views:
    681
    Robert Posey
    Nov 26, 2003
  2. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,377
    Henrik
    Aug 10, 2005
  3. ashu
    Replies:
    1
    Views:
    464
  4. ashu
    Replies:
    2
    Views:
    619
    mysticlol
    Nov 6, 2006
  5. Xin Xiao

    Block RAM Distributed RAM

    Xin Xiao, Jan 7, 2008, in forum: VHDL
    Replies:
    8
    Views:
    1,478
    Duane Clark
    Jan 7, 2008
Loading...

Share This Page