473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

module imports and memory usage

When memory usage is a concern, is it better to do:

from X import Y

or

import X

Also, is there a way to load and unload modules as they are needed. I
have some scripts that sleep for extended periods during a while loop
and I need to be as memory friendly as possible. I can post a detailed
script that currently uses ~ 10MB of memory if anyone is interested.

Thanks,
Brad
Jul 18 '05 #1
4 8246
Brad Tilley wrote:

Also, is there a way to load and unload modules as they are needed. I
have some scripts that sleep for extended periods during a while loop
and I need to be as memory friendly as possible. I can post a detailed
script that currently uses ~ 10MB of memory if anyone is interested.


I believe that the rule of thumb for Python, C, Java
and probably most other portable languages is that
freed memory cannot be released back to the
operating system. The best you can expect (usually true)
is that allocating the same amount later will not
increase the memory footprint.

So the only way to write memory efficient programs
might be to:
1. either not load all the data
2. run the data intensive routines as a separate process
that terminates and exists

Istvan.
Jul 18 '05 #2
Brad Tilley <br********@gmail.com> writes:
When memory usage is a concern, is it better to do:

from X import Y

or

import X
Depending on "Y", the latter can technically use less memory, but it's
likely to be fairly small and depends on how many symbols from that
module you want to have local bindings. In general, it's not worth
worrying about.

For example, if module X has 1000 bound names in it, then doing an
"import X" loads the module and gives your current namespace a single
reference to the entire module. If however, you do a "from X import
*" you still load the X module, but also get local names bound for
each of the names in the X module. So you have double the references
to the objects created within X. Those references take up space (for
the textual name and the pointer) in your current namespace's
dictionary.

But if you just do a "from X import Y" where Y is a single symbol,
then it's a wash because you get Y bound as a local namespace name,
but you don't have a reference to the module itself. If you were to
import multiple symbols, then you'd have a few extra references locally.

Sometimes though it's not even the memory you can affect, but
performance. One place where I did find this to be noticeable, for
example, was with wxPython, where older releases typically often did a
"from wxPython.wx import *" since all the wxPython names already had a
"wx" prefix. But that yielded thousands of extra name bindings in the
local namespace. Switching to "from wxPython import wx" and then
using "wx.XXX" instead of just "XXX" actually made a fairly dramatic
decrease in load time. It did also drop memory because I had a bunch
of plugin modules, all of which were burning up a few thousand name
bindings for the same wxPython symbols. Switching them to just use
the module reference was a noticeable savings in that case.
Also, is there a way to load and unload modules as they are needed. I
have some scripts that sleep for extended periods during a while loop
and I need to be as memory friendly as possible. I can post a detailed
script that currently uses ~ 10MB of memory if anyone is interested.


You can play tricks by manually deleting a module out of sys.modules,
but there's no guarantee that the memory will actually be released
(either by Python or the OS). Unless you're working under very
specific external resources, I'd generally leave this to the OS. It
will figure out when some of your working set is unused for extended
periods and generally it should end up in swap space if you actually
need the memory for something else.

-- David
Jul 18 '05 #3
Brad Tilley wrote:
When memory usage is a concern, is it better to do:

from X import Y

or

import X

Also, is there a way to load and unload modules as they are needed. I
have some scripts that sleep for extended periods during a while loop
and I need to be as memory friendly as possible. I can post a detailed
script that currently uses ~ 10MB of memory if anyone is interested.

Thanks,
Brad


I discovered that when I wrap my code up in a function def and call it
that it uses around 4.6 MB of RAM all the time... even while sleeping.
However, when I don't put it in a function def it uses around 2.6MB of
data when it executes and only 100KB while sleeping. Why is this?
Jul 18 '05 #4
On Tue, 30 Nov 2004 16:46:43 -0500, Brad Tilley <br********@gmail.com> wrote:
I discovered that when I wrap my code up in a function def and call it
that it uses around 4.6 MB of RAM all the time... even while sleeping.
However, when I don't put it in a function def it uses around 2.6MB of
data when it executes and only 100KB while sleeping. Why is this?


Totally unchecked, popped up from somewhere between my ears.

Is it possible that (in some situations) the function object keeps a
reference to its locals() somewhere, in such a way that it never gets
collected? Just wondering...
--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmail.com
mail: ca********@yahoo.com
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brad Parks | last post by:
Are there any performance or memory-usage benefits of placing public functions and subroutines in a Class Module rather than a standard module?
10
by: Ron | last post by:
Hello, The following code works in a Form class module but not a standard module: Dim d1 As DateTime Format(d1, "MMMM") In a standard module I get a squigly line saying that an argument has...
2
by: jimmyfishbean | last post by:
Hi, I have created a VB.Net windows service. Everything was working fine until I attempted to add a setup project. Now, for some strange, when I try and run the application (using INSTALLUTIL...
6
by: mwt | last post by:
When I'm rewriting code (cutting and pasting pieces from earlier modules), is there a quick way to determine if I have imported all the necessary modules? I am used to working in Java, where the...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
3
by: ThunderMusic | last post by:
Hi, I want to know if having to many using (imports in VB) statements at the top of a class or page can hit on the performance of the application or on the memory usage of the application. I...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
6
by: Silfheed | last post by:
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for...
0
by: arobinson | last post by:
I am a developer relatively new to python and brand new to pygtk development. I wanted to write a bloglines tray icon notifier for my linux box. Everything is working quite well functionality wise,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.