Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Python `Memory Error` with list `append()` lots of RAM left

Tags:

I am building a large data dictionary from a set of text files. As I read in the lines and process them, I append(dataline) to a list.

At some point the append() generates a Memory Error exception. However, watching the program run in the Windows Task Manager, at the point of the crash I see 4.3 GB available and 1.1 GB free.

Thus, I do not understand the reason for the exception.

Python version is 2.6.6. I guess, the only reason is that it is not able to use more of the available RAM. If this is so, is it possible to increase the allocation?

like image 508
Pete Avatar asked Dec 14 '10 17:12

Pete


People also ask

How do I fix out of memory error in Python?

In such cases, we can use the conda install command in python prompt and install those packages to fix the Memory Error. Another type of Memory Error occurs when the memory manager has used the Hard disk space of our system to store the data that exceeds the RAM capacity.

How much memory does a list take up Python?

When you create a list object, the list object by itself takes 64 bytes of memory, and each item adds 8 bytes of memory to the size of the list because of references to other objects.

Why Python memory consumption is high?

Those numbers can easily fit in a 64-bit integer, so one would hope Python would store those million integers in no more than ~8MB: a million 8-byte objects. In fact, Python uses more like 35MB of RAM to store these numbers. Why? Because Python integers are objects, and objects have a lot of memory overhead.


2 Answers

If you're using a 32-bit build of Python, you might want to try a 64-bit version.

It is possible for a process to address at most 4GB of RAM using 32-bit addresses, but typically (depending on the OS), one gets much less. It sounds like your Python process may be hitting this limit. 64-bit addressing removes this limitation.

edit Since you're asking about Windows, the following page is of relevance: Memory Limits for Windows Releases. As you can see, the limit per 32-bit process is 2, 3 or 4GB depending on the OS version and configuration.

like image 91
NPE Avatar answered Oct 22 '22 16:10

NPE


If you're open to restructuring the code instead of throwing more memory at it, you might be able to get by with this:

data = (processraw(raw) for raw in lines) 

where lines is either a list of lines or file.xreadlines() or similar.

like image 42
nmichaels Avatar answered Oct 22 '22 17:10

nmichaels