reading files

Mike Meyer mwm at mired.org
Mon Dec 19 12:20:57 EST 2005


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> The text you read is still hanging around, waiting for you to use it. Once
> you are done with it, you might want to reclaim that memory used,
> especially if it is a 100MB text file:
>
> mytext = ""  # re-bind the name 'mytext' to the empty string

If you have no further use for the variable, then "del mytext" will
also free up the memory, and make it clear to the reader that you're
done with the memory.

> But even that is not always needed: if the name 'mytext' is local to a
> function, then when the function ends, the block of memory used will be
> collected by the Python garbage collector, and you don't have to do a
> thing. 

The immediate collection is implementation-dependent. It works that
way in Cpython. Other implementations may leave the memory allocated
unil there's a GC run. Since I'm dealinng with trivia, whether or not
the OS gets the memory back or it stays part of the python process
varies from platform to platform and implementation to implementation.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list