← Back to Python

All Topics

Advertisement

Learn/Python/Advanced Python

Memory Management

Topic: Performance

Advertisement

Introduction

Understanding and optimizing Python memory usage for better performance.

Memory Profiling

# Basic memory usage
import sys

lst = [1, 2, 3]
print(sys.getsizeof(lst))  # Size in bytes

# Memory profiler
# pip install memory_profiler
from memory_profiler import profile

@profile
def my_function():
    data = [i ** 2 for i in range(100000)]
    return data

Garbage Collection

import gc

# Collect garbage
gc.collect()

# Get garbage collection stats
print(gc.get_stats())

# Find unreachable objects
print(gc.garbage)

# Disable automatic collection
gc.disable()
gc.enable()

Memory Efficient Patterns

# Use generators instead of lists
def get_lines(file):
    for line in open(file):
        yield line

# Use __slots__ in classes
class Point:
    __slots__ = ["x", "y"]
    
# Use arrays for numerical data
from array import array
arr = array("i", [1, 2, 3, 4, 5])

Practice Problems

  1. Profile memory usage
  2. Find memory leaks
  3. Optimize data structures
  4. Use generators for large files
  5. Implement slots in classes

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →