Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Handle the MemoryError in Python
Next article icon

How to Handle the MemoryError in Python

Last Updated : 01 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

One common issue developers may encounter is the dreaded MemoryError. This error occurs when a program runs out of available memory, causing it to crash. In this article, we will explore the causes of MemoryError, discuss common scenarios leading to this error, and present effective strategies to handle and prevent it.

What is MemoryError in Python?

A MemoryError in Python is an exception that occurs when the interpreter detects that the program has attempted to allocate more memory than is available. This typically happens when a Python script or program tries to create or manipulate data structures that consume more memory than the system can provide.

Syntax :

error MemoryError

Why does MemoryError Occur in Python?

Below, are the reasons for the MemoryError in Python.

  • Infinite For Loop
  • Large Data Structure
  • Function with No Base Case

Infinite For Loop

In below, code the function create_large_list attempts to create an infinite list, leading to an unrestrained increase in memory usage. This results in a MemoryError as the program exhausts available memory, causing termination due to the unbounded growth of the list within the infinite loop.

Python3
def create_large_list():     large_list = []     while True:         large_list.append('data')  create_large_list() 

Large Data Structure

In below code , the `consume_memory` function creates an ever-growing string in an infinite loop, leading to excessive memory consumption. This unbounded growth eventually triggers a `MemoryError` as the system runs out of memory.

Python3
def consume_memory():     data = 'a' * (10**8)       while True:         data += data  consume_memory() 

Function with No Base Case

In this example, in below code the `recursive_function` lacks a base case, resulting in an infinite recursion that leads to a `MemoryError`. Without a termination condition, the function continuously consumes stack space, exhausting available memory.

Python3
def recursive_function(n):     return n + recursive_function(n + 1)  recursive_function(1) 

Approaches/Reasons to Solve

Below, are the reason to solve MemoryError in Python.

  • Properly Terminate the Loop
  • Efficient Memory Usage
  • Add Base Case to Function

Properly Terminate the Loop

below solution to memory-related issues in create_large_list involves limiting the loop iterations to a reasonable number (in this case, 10^6). By doing so, the function avoids creating an infinitely growing list, preventing excessive memory consumption and potential MemoryError.

Python3
def create_large_list():     large_list = []     for _ in range(10**6):  # Limiting the loop iterations         large_list.append('data')  create_large_list() 

Efficient Memory Usage

In the solution for `consume_memory`, the code addresses memory concerns by reducing the size of the string to 'a' * (10^6). Although the loop is infinite, the memory footprint is now controlled, mitigating the risk of a `MemoryError`. This optimization ensures more efficient memory usage.

Python3
def consume_memory():     data = 'a' * (10**6)  # Reducing the size of the string     while True:         data += data  consume_memory() 

Add Base Case to Function

In below code , the solution for `recursive_function` involves adding a base case (`n > 10^6`) to terminate the recursion, preventing an infinite loop and potential `MemoryError`. With this modification, the function now has a clear stopping condition, ensuring controlled memory usage and a stable execution.

Python3
def recursive_function(n):     if n > 10**6:  # Adding a base case to terminate recursion         return 0     return n + recursive_function(n + 1)  recursive_function(1) 

Conclusion

In conclusion, effectively handling MemoryError in Python requires a proactive approach to memory management. By employing profiling tools, optimizing data structures, and implementing resource release strategies, developers can identify and address excessive memory consumption. Efficient coding practices and preventive measures, like limiting data loading and terminating infinite loops, enhance Python application stability, mitigating memory-related errors.


Next Article
How to Handle the MemoryError in Python

S

susobhanakhuli
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Geeks Premier League 2023
  • Python Errors
Practice Tags :
  • python

Similar Reads

    How to handle KeyError Exception in Python
    In this article, we will learn how to handle KeyError exceptions in Python programming language. What are Exceptions?It is an unwanted event, which occurs during the execution of the program and actually halts the normal flow of execution of the instructions.Exceptions are runtime errors because, th
    3 min read
    Handle Memory Error in Python
    One common issue that developers may encounter, especially when working with loops, is a memory error. In this article, we will explore what a memory error is, delve into three common reasons behind memory errors in Python for loops, and discuss approaches to solve them. What is a Memory Error?A mem
    3 min read
    How to Explicitly Free Memory in Python?
    Python uses a technique called garbage collection to automatically manage memory. The garbage collector identifies objects that are no longer in use and reclaims their memory. The primary mechanism for this is reference counting, augmented by a cyclic garbage collector to handle reference cycles. Wh
    3 min read
    How to Limit Heap Size in Python?
    In Python, the heap size is managed automatically by the interpreter and the Garbage Collector (GC), which makes Python simpler than low-level languages like C or C++. Python doesn't provide direct way to limit Heap Memory. However, there are ways to limit the heap size if you are working on systems
    3 min read
    How to Avoid "CUDA Out of Memory" in PyTorch
    When working with PyTorch and large deep learning models, especially on GPU (CUDA), running into the dreaded "CUDA out of memory" error is common. This issue can disrupt training, inference, or testing, particularly when dealing with large datasets or complex models. In this article, we’ll explore s
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences