module imports and memory usage

Brad Tilley bradtilley at gmail.com
Tue Nov 30 10:39:52 EST 2004


Jp Calderone wrote:
> 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

Thanks for the info JP. No, I'm not sure that I have good reason to be 
concerned over memory. I usually think in terms of percentages. 10MB is 
an insignificant percentage on a machine that has 1GB of RAM, but 
perhaps more significant when only 64MB or 128MB is present.



More information about the Python-list mailing list