MemoryError
MemoryError
Traceback
Traceback (most recent call last):
File "main.py", line 1, in <module>
huge_list = [0] * (10 ** 12)
MemoryErrorWhat causes this error
Python tried to allocate more memory than is available. This happens with extremely large data structures, loading huge files into memory, or repeated memory allocation without cleanup.
How to fix it
Use generators instead of lists for large sequences. Process files in chunks rather than reading entirely. Use memory-efficient data structures. Consider 64-bit Python for large datasets. Profile memory with `tracemalloc`.
Code that causes this error
huge_list = [0] * (10 ** 12)
Fixed code
def huge_range():
for i in range(10 ** 12):
yield i
for val in huge_range():
if val > 10:
breakAbout MemoryError
A MemoryError is raised when an operation runs out of memory and the situation is still potentially recoverable. This happens when attempting to allocate more memory than is available — for example, creating an extremely large list, loading a massive file entirely into memory, or performing operations that require more working memory than the system can provide. Note that not all out-of-memory situations result in a clean MemoryError; sometimes the operating system's OOM killer terminates the process first.
To handle large datasets, use generators and iterators instead of materializing entire collections, process files line by line instead of reading them whole, and use memory-mapped files (`mmap`) or specialized libraries like `pandas` with chunked reading. In 32-bit Python builds, the address space limit (~2-4 GB) can trigger MemoryError even when the system has more RAM available.
Common scenarios
Infinite recursion from missing or incorrect base cases
Modifying dictionaries or sets during iteration
Calling generators or coroutines in unsupported ways
Using asyncio event loops incorrectly or attempting to nest them