You’re the Expert!

pynfinity

Build mountain with each pebble 🧗

Topics | Stepping Stones

📚 Guides

🌍 Pebbles & Contributions

✍️ Write a Post
🗺️ Folium Maps
🗺️ Folium Maps
⏳ Time Series Resampling
⏳ Time Series Resampling
📦 Dataclasses
📦 Dataclasses
🔁 Itertools
🔁 Itertools
🗄️ SQLAlchemy Basics
🗄️ SQLAlchemy Basics
📄 JSON Handling
📄 JSON Handling
🖥️ OS Module
🖥️ OS Module
🔢 NumPy Arrays
🔢 NumPy Arrays
⚡ Generators
⚡ Generators
🐼 Pandas DataFrames
🐼 Pandas DataFrames
🎛️ Multiprocessing
🎛️ Multiprocessing
⚡ FastAPI Endpoints
⚡ FastAPI Endpoints
📜 List Comprehensions
📜 List Comprehensions
🪵 Logging
🪵 Logging
🚀 Streamlit Apps
🚀 Streamlit Apps
🧪 Pytest Testing
🧪 Pytest Testing
🧹 Data Cleaning with Dropna
🧹 Data Cleaning with Dropna
🧠 TensorFlow Basics
🧠 TensorFlow Basics
🌲 Git Basics
🌲 Git Basics
📦 Virtual Environments
📦 Virtual Environments
🏷️ Type Hinting
🏷️ Type Hinting
🚪 Context Managers
🚪 Context Managers
🛠️ Functools
🛠️ Functools
🤖 Scikit-Learn Linear Regression
🤖 Scikit-Learn Linear Regression
🧩 Regular Expressions
🧩 Regular Expressions
🎀 Decorators
🎀 Decorators
📸 OpenCV Image Reading
📸 OpenCV Image Reading
💻 Argparse CLI
💻 Argparse CLI
✨ Jupyter Magic Commands
✨ Jupyter Magic Commands
🛡️ Pydantic Models
🛡️ Pydantic Models
🕸️ Web Scraping with BeautifulSoup
🕸️ Web Scraping with BeautifulSoup
📊 Interactive Plots with Plotly
📊 Interactive Plots with Plotly
📚 Collections Module
📚 Collections Module
📝 NLTK Tokenization
📝 NLTK Tokenization
λ Lambda Functions
λ Lambda Functions
📉 Matplotlib Plotting
📉 Matplotlib Plotting
🔥 Seaborn Heatmaps
🔥 Seaborn Heatmaps
🧵 Multithreading
🧵 Multithreading
⏳ AsyncIO
⏳ AsyncIO
🔥 PyTorch Tensors
🔥 PyTorch Tensors
⚙️ Sys Module
⚙️ Sys Module
☁️ AWS S3 with Boto3
☁️ AWS S3 with Boto3
🥒 Pickle Serialization
🥒 Pickle Serialization
🕸️ NetworkX Graphs
🕸️ NetworkX Graphs
➗ Math Module
➗ Math Module
🔍 Exploratory Data Analysis (EDA)
🔍 Exploratory Data Analysis (EDA)
📊 CSV Processing
📊 CSV Processing
🐳 Dockerfiles
🐳 Dockerfiles
📉 Statsmodels OLS
📉 Statsmodels OLS
+ New Post

🧠 Memory Management in Python

Python handles memory management automatically, but understanding how it works helps in writing efficient code and avoiding memory leaks.


♻️ Reference Counting

The primary mechanism for memory management in Python is reference counting.
- Every object has a counter.
- The counter increments when a reference is created.
- The counter decrements when a reference is deleted or goes out of scope.
- When the counter reaches 0, the memory is reclaimed.

import sys
a = []
b = a
print(sys.getrefcount(a)) # 3 (a, b, and the argument to getrefcount)

🗑 Garbage Collection (GC)

Reference counting cannot handle cyclic references (e.g., A refers to B, and B refers to A). Python has a cyclic garbage collector to handle this.

The gc module allows you to interact with the garbage collector.

import gc

# Force a collection
gc.collect()

# Disable GC
gc.disable()

🔗 Weak References

Weak references allow you to refer to an object without increasing its reference count. This is useful for caches.

import weakref

class MyClass:
    pass

obj = MyClass()
r = weakref.ref(obj)

print(r()) # <__main__.MyClass object ...>
del obj
print(r()) # None (object has been collected)

⚠️ __del__ Pitfalls

The __del__ method is called when an object is about to be destroyed. However, rely on it with caution:
- It might not be called immediately.
- Exceptions in __del__ are ignored.
- It can interfere with garbage collection of cycles (in older Python versions).

Use Context Managers (with statement) for resource cleanup instead.


📝 Summary

  • Ref Counting: Main mechanism. Fast and real-time.
  • Cyclic GC: Handles reference cycles.
  • Weak Refs: Reference without ownership.
  • Best Practice: Avoid __del__, use context managers.

Created with ❤️ by Pynfinity



Pynfinity
Install Pynfinity Add to home screen for the best experience