Resource and Memory Management Principles
Universal Resource Management Rules
1. Always Clean Up Resources
Resources requiring cleanup:
- Files, network connections, database connections
- Locks, semaphores, mutexes
- Memory allocations (in manual-memory languages)
- OS handles, GPU resources
Clean up in ALL paths:
- Success path: Normal completion
- Error path: Exception thrown, error returned
- Early return path: Guard clauses, validation failures
Use language-appropriate patterns:
- Go: defer statements
- Rust: Drop trait (RAII)
- Python: context managers (with statement)
- TypeScript: try/finally
- Java: try-with-resources
2. Timeout All I/O Operations
Why timeout:
- Network requests can hang indefinitely
- Prevents resource exhaustion (connections, threads)
- Provides predictable failure behavior
Timeout recommendations:
- Network requests: 30s default, shorter (5-10s) for interactive
- Database queries: 10s default, configure per query complexity
- File operations: Usually fast, but timeout on network filesystems
- Message queue operations: Configurable, avoid indefinite blocking
3. Pool Expensive Resources
Resources to pool:
- Database connections: Pool size 5-20 per app instance
- HTTP connections: Reuse with keep-alive
- Thread pools: Size based on CPU count (CPU-bound) or I/O wait (I/O-bound)
Benefits:
- Reduces latency (no connection setup overhead)
- Limits resource consumption (cap on max connections)
- Improves throughput (reuse vs create new)
Connection Pool Best Practices:
- Minimum connections: 5 (ensures pool is warm)
- Maximum connections: 20-50 (prevents overwhelming database)
- Idle timeout: Close connections idle >5-10 minutes
- Validation: Test connections before use (avoid broken connections)
- Monitoring: Track utilization, wait times, timeout rates
4. Avoid Resource Leaks
What is a leak:
- Acquire resource (open file, allocate memory, get connection)
- Never release it (forget to close, exception prevents cleanup)
- Eventually exhaust system resources (OOM, max connections, file descriptors)
Detection:
- Monitor open file descriptors, connection counts, memory usage over time
- Run long-duration tests, verify resource counts stay stable
- Use leak detection tools (valgrind, ASan, heap profilers)
Prevention:
- Use language patterns that guarantee cleanup (RAII, defer, context managers)
- Never rely on manual cleanup alone (use language features)
5. Handle Backpressure
Problem: Producer faster than consumer
- Queue grows unbounded → memory exhaustion
- System becomes unresponsive under load
Solutions:
- Bounded queues: Fixed size, block or reject when full
- Rate limiting: Limit incoming request rate
- Flow control: Consumer signals producer to slow down
- Circuit breakers: Stop accepting requests when overwhelmed
- Drop/reject: Fail fast when overloaded (better than crashing)
Memory Management by Language Type
Garbage Collected (Go, Java, Python, JavaScript, C#):
- Memory automatically freed by GC
- Still must release non-memory resources (files, connections, locks)
- Be aware of GC pauses in latency-sensitive applications
- Profile memory usage to find leaks (retained references preventing GC)
Manual Memory Management (C, C++):
- Explicit malloc/free or new/delete
- Use RAII pattern in C++ (Resource Acquisition Is Initialization)
- Avoid manual management in modern C++ (use smart pointers: unique_ptr, shared_ptr)
Ownership-Based (Rust):
- Compiler enforces memory safety at compile time
- No GC pauses, no manual management
- Ownership rules prevent leaks and use-after-free automatically
- Use reference counting (Arc, Rc) for shared ownership
Related Principles
- Concurrency and Threading Mandate @.claude/rules/concurrency-and-threading-mandate.md
- Concurrency and Threading Principles @.claude/skills/concurrency-and-threading-principles/SKILL.md
- Error Handling Principles @.claude/rules/error-handling-principles.md - Resource cleanup in error paths
1---2name: resources-and-memory-management-33description: >-4 Apply resource lifecycle management patterns when working with files, database connections, network sockets, locks, or any resource requiring explicit cleanup. Covers RAII, defer/finally, connection pooling, graceful shutdown, and leak prevention.5user-invocable: false6metadata:7 source: https://skillmd.com/skills/irahardianto/resources-and-memory-management-38---910## Resource and Memory Management Principles1112### Universal Resource Management Rules1314**1. Always Clean Up Resources**1516**Resources requiring cleanup:**1718- Files, network connections, database connections 19- Locks, semaphores, mutexes 20- Memory allocations (in manual-memory languages) 21- OS handles, GPU resources2223**Clean up in ALL paths:**2425- Success path: Normal completion 26- Error path: Exception thrown, error returned 27- Early return path: Guard clauses, validation failures2829**Use language-appropriate patterns:**3031- Go: defer statements 32- Rust: Drop trait (RAII) 33- Python: context managers (with statement) 34- TypeScript: try/finally 35- Java: try-with-resources3637**2. Timeout All I/O Operations**3839**Why timeout:**4041- Network requests can hang indefinitely 42- Prevents resource exhaustion (connections, threads) 43- Provides predictable failure behavior4445**Timeout recommendations:**4647- Network requests: 30s default, shorter (5-10s) for interactive 48- Database queries: 10s default, configure per query complexity 49- File operations: Usually fast, but timeout on network filesystems 50- Message queue operations: Configurable, avoid indefinite blocking5152**3. Pool Expensive Resources**5354**Resources to pool:**5556- Database connections: Pool size 5-20 per app instance 57- HTTP connections: Reuse with keep-alive 58- Thread pools: Size based on CPU count (CPU-bound) or I/O wait (I/O-bound)5960**Benefits:**6162- Reduces latency (no connection setup overhead) 63- Limits resource consumption (cap on max connections) 64- Improves throughput (reuse vs create new)6566**Connection Pool Best Practices:**6768- Minimum connections: 5 (ensures pool is warm) 69- Maximum connections: 20-50 (prevents overwhelming database) 70- Idle timeout: Close connections idle >5-10 minutes 71- Validation: Test connections before use (avoid broken connections) 72- Monitoring: Track utilization, wait times, timeout rates7374**4. Avoid Resource Leaks**7576**What is a leak:**7778- Acquire resource (open file, allocate memory, get connection) 79- Never release it (forget to close, exception prevents cleanup) 80- Eventually exhaust system resources (OOM, max connections, file descriptors)8182**Detection:**8384- Monitor open file descriptors, connection counts, memory usage over time 85- Run long-duration tests, verify resource counts stay stable 86- Use leak detection tools (valgrind, ASan, heap profilers)8788**Prevention:**8990- Use language patterns that guarantee cleanup (RAII, defer, context managers) 91- Never rely on manual cleanup alone (use language features)9293**5. Handle Backpressure**9495**Problem:** Producer faster than consumer9697- Queue grows unbounded → memory exhaustion 98- System becomes unresponsive under load99100**Solutions:**101102- Bounded queues: Fixed size, block or reject when full 103- Rate limiting: Limit incoming request rate 104- Flow control: Consumer signals producer to slow down 105- Circuit breakers: Stop accepting requests when overwhelmed 106- Drop/reject: Fail fast when overloaded (better than crashing)107108### Memory Management by Language Type109110**Garbage Collected (Go, Java, Python, JavaScript, C#):**111112- Memory automatically freed by GC 113- Still must release non-memory resources (files, connections, locks) 114- Be aware of GC pauses in latency-sensitive applications 115- Profile memory usage to find leaks (retained references preventing GC)116117**Manual Memory Management (C, C++):**118119- Explicit malloc/free or new/delete 120- Use RAII pattern in C++ (Resource Acquisition Is Initialization) 121- Avoid manual management in modern C++ (use smart pointers: unique_ptr, shared_ptr)122123**Ownership-Based (Rust):**124125- Compiler enforces memory safety at compile time 126- No GC pauses, no manual management 127- Ownership rules prevent leaks and use-after-free automatically 128- Use reference counting (Arc, Rc) for shared ownership129130### Related Principles131- Concurrency and Threading Mandate @.claude/rules/concurrency-and-threading-mandate.md132- Concurrency and Threading Principles @.claude/skills/concurrency-and-threading-principles/SKILL.md133- Error Handling Principles @.claude/rules/error-handling-principles.md - Resource cleanup in error paths