Memory Management Skill
Purpose
Keep memory usage predictable and within limits.
Python Techniques
- Generators: Stream data instead of loading all
- Context Managers: Ensure cleanup (
withstatements) - Weak References: Avoid reference cycles
- Object Pools: Reuse expensive objects
- Profiling:
tracemalloc,memory_profiler
GPU Techniques
- Gradient Checkpointing: Trade compute for memory
- Mixed Precision: FP16/FP8 reduces memory 2x
- Batch Size: Tune for memory limit
- Clear Cache:
torch.cuda.empty_cache()between tasks - Model Sharding: Split across GPUs
CF-Specific
- Don't store large blobs in
task["result"] - Use
output_path/for large assets - Stream video/audio when possible
- Clear GPU memory between pipeline kinds
- Monitor worker memory in K8s
Targets
| Component | Limit | Alert |
|---|---|---|
| Worker pod | 80% of request | At 70% |
| GPU VRAM | 90% of capacity | At 85% |
| Redis | 80% of maxmemory | At 75% |
Quality Gates
- No memory leaks (stable over 24h)
- Large objects streamed, not loaded
- GPU memory cleared between unrelated tasks
- Memory usage within K8s limits