Please help with MemoryError

Alf P. Steinbach alfps at start.no
Thu Feb 11 19:03:43 EST 2010


* Jeremy:
> I have been using Python for several years now and have never run into
> memory errors…
> 
> until now.
> 
> My Python program now consumes over 2 GB of memory and then I get a
> MemoryError.  I know I am reading lots of files into memory, but not
> 2GB worth.  I thought I didn't have to worry about memory allocation
> in Python because of the garbage collector.  On this note I have a few
> questions.  FYI I am using Python 2.6.4 on my Mac.
> 
> 1.    When I pass a variable to the constructor of a class does it
> copy that variable or is it just a reference/pointer?  I was under the
> impression that it was just a pointer to the data.

Uhm, I discovered that "pointer" is apparently a Loaded Word in the Python 
community. At least in some sub-community, so, best avoided. But essentially 
you're just passing a reference to an object. The object is not copied.


> 2.    When do I need to manually allocate/deallocate memory and when
> can I trust Python to take care of it?

Python takes care of deallocation of objects that are /no longer referenced/.


> 3.    Any good practice suggestions?

You need to get rid of references to objects before Python will garbage collect 
them.

Typically, in a language like Python (or Java, C#...) memory leaks are caused by 
keeping object references in singletons or globals, e.g. for purposes of event 
notifications. For example, you may have some dictionary somewhere.

Such references from singletons/globals need to be removed.

You do not, however, need to be concerned about circular references, at least 
unless you need some immediate deallocation.

For although circular references will prevent the objects involved from being 
immediately deallocated, the general garbage collector will take care of them later.



Cheers & hth.,

- Alf



More information about the Python-list mailing list