Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question - remove a module from ram

Reply
Thread Tools

newbie question - remove a module from ram

 
 
john fabiani
Guest
Posts: n/a
 
      05-10-2004
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
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      05-10-2004
"john fabiani" <> wrote in message
news:4AAnc.6791$ m...
> 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


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-10-2004
Paul McGuire wrote:

> "john fabiani" <> wrote in message
> news:4AAnc.6791$ m...
>> 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

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      05-10-2004
"Peter Otten" <__peter__@web.de> wrote in message
news:c7nvr1$fsc$01$...
> Paul McGuire wrote:
>
> > "john fabiani" <> wrote in message
> > news:4AAnc.6791$ m...
> >> 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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      05-10-2004
Paul McGuire wrote:

> "Peter Otten" <__peter__@web.de> wrote in message
> news:c7nvr1$fsc$01$...
>> Paul McGuire wrote:
>>
>> > "john fabiani" <> wrote in message
>> > news:4AAnc.6791$ m...
>> >> 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:0
[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

 
Reply With Quote
 
john fabiani
Guest
Posts: n/a
 
      05-10-2004
Paul McGuire wrote:
> "Peter Otten" <__peter__@web.de> wrote in message
> news:c7nvr1$fsc$01$...
>
>>Paul McGuire wrote:
>>
>>
>>>"john fabiani" <> wrote in message
>>>news:4AAnc.6791$ .com...
>>>
>>>>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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      05-10-2004

"john fabiani" <> wrote in message
news:4AAnc.6791$ m...
> 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




 
Reply With Quote
 
Michael Hudson
Guest
Posts: n/a
 
      05-10-2004
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
 
Reply With Quote
 
john fabiani
Guest
Posts: n/a
 
      05-10-2004
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
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      05-10-2004
Terry Reedy wrote:

> "john fabiani" <> wrote in message
> news:4AAnc.6791$ m...
>
>>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
 
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
Help, no RAM, and ramfreer is err'ing, need freware RAM giver Morph Computer Information 4 10-10-2004 09:42 AM
No RAM left, n e 1 no of RAM giver (freeware) ? Morph Computer Support 2 10-09-2004 02:11 AM
NEW RAM V OLD RAM Allan Birnbaum Ditlevsen Computer Information 2 06-07-2004 01:50 PM
Looking for a VHDL or Verilog RAM Model that modles Common RAM Faults Robert Posey VHDL 0 11-26-2003 07:50 PM
Mixing SDR Ram with DDR Ram? Daz Computer Support 2 09-14-2003 03:28 PM



Advertisments