module imports and memory usage

Jp Calderone exarkun at divmod.com
Tue Nov 30 10:22:02 EST 2004


On Tue, 30 Nov 2004 10:02:27 -0500, Brad Tilley <bradtilley at gmail.com> wrote:
>When memory usage is a concern, is it better to do:
> 
> from X import Y
> 
> or
> 
> import X

  There is no difference.  If you are concerned about memory usage, you probably need to take a look at the data structures holding your application's data, and find a way to make them smaller (most commonly, this is done by selecting a new algorithm that works on a sparser data structure).  This isn't specific to Python, of course.

> 
> 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.

  Not really, no.  Deleting all references to a module may cause the module object to be collected by the vm, but there is no guarantee that the memory it used will be released to the platform.  It's possible Python will re-use it for the next object it needs memory for, which may help your memory problems if you are loading many (_many_) more modules than you ever use at once.

  On a reasonable platform, if your program is sleeping, much of the memory it uses will be swapped out, especially if other processes need it.  Are you sure you have good reason to be concerned over the size of the program?

  Jp



More information about the Python-list mailing list