Part 4: Resource and Performance Errors
Introduction
Resource Errors
What Are Resource Errors?
Real-World Examples from My Projects
Memory Leaks from Unclosed Files
# Incorrect - Files not properly closed
def process_all_logs(log_directory):
"""Process all log files in directory"""
log_files = []
for filename in os.listdir(log_directory):
if filename.endswith('.log'):
# Bug: Opening files but never closing them
f = open(os.path.join(log_directory, filename), 'r')
log_files.append(f)
# Process files
for log_file in log_files:
process_log_content(log_file.read())
# Files never closed - memory leak and file handle exhaustion
# With 1000s of log files, this exhausts file handles
process_all_logs('/var/logs/app/') # Eventually: OSError: Too many open filesLoading Large Datasets into Memory
Connection Pool Exhaustion
Database Connection Leaks
How I Prevent Resource Errors
1. Always Use Context Managers
2. Implement Resource Limits
3. Monitor Resource Usage
Time Limit Exceeded Errors
What Are Time Limit Errors?
Real-World Examples
Inefficient Algorithm
Blocking I/O Operations
Unbounded Recursive Calls
Database Query Performance
How I Prevent Performance Errors
1. Profile Before Optimizing
2. Use Appropriate Data Structures
3. Implement Caching
4. Add Timeouts to All External Calls
Tools I Use
Resource Monitoring
Performance Testing
Key Takeaways
Next in Series
Last updated