You’re the Expert!

pynfinity

Build mountain with each pebble 🧗

Topics | Stepping Stones

📚 Guides

🌍 Pebbles & Contributions

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