473,769 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8446
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********@gma il.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********@gma il.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********@gmai l.com
mail: ca********@yaho o.com
Jul 18 '05 #5

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

Similar topics

4
13905
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
1915
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 not been specified for parameter format... So I tried this:
2
2755
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 to install/uninstall it) I get the error: (53) The specified module could not be found When I debug the code, this error is thrown when the following code
6
1154
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 compiler will tell you if you haven't imported everything, and also Eclipse, which has the handy "organize imports" feature. This is not the case in Python, since it's not compiled, of course, and also running it might not insure you've got all the...
10
3776
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 be run as commands to a file with no extension. This allows other programs to use the command as an interface, and I can re-write the program in some other language
3
1721
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 use ASP.NET framework v2.0... When creating a web page there are about 10-15 using statements at the top, should I leave them there or remove all those the page doesn't use? Thanks
32
5826
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 classes in a Python module/file. What is the motivation behind it, would it be a bad idea to have a guideline in your project that promotes a one class per file structure (assuming most of the programmers a background similar to mine)?
6
3471
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 testee.py without changing the code for testee.py. testee.py has a module in it that we want to mock in some tests and in others use the real module. /foo.py: (real module) class bar:
0
1067
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, but the memory footprint is horrible. This little tray icon that simply has a thread for updates, a gtk.StatusIcon, a few relatively small dict objects and a context menu is running at around 20-25 MB of memory. Doing some initial research,...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.