This Claude skill provides comprehensive expertise in memory management, enabling developers to debug leaks, profile usage, and optimize allocations across C++ and Python environments. It leverages industry-standard tools like Valgrind, ASAN, and tracemalloc to resolve OOM errors, eliminate memory bottlenecks, and enhance overall system stability and performance.
Use Cases
Detecting and fixing 'definitely lost' or 'possibly lost' memory leaks in C++ applications using Valgrind and AddressSanitizer.
Profiling Python heap usage to identify memory-intensive objects and reference cycles using tracemalloc and objgraph.
Refactoring legacy manual memory management into modern RAII patterns and smart pointer ownership to prevent resource leaks.
Optimizing high-frequency allocation paths in performance-critical systems through object pooling and arena allocation strategies.
Troubleshooting and resolving 'Out of Memory' (OOM) crashes by analyzing heap growth patterns and fragmentation in production environments.
name
memory-management-optimization
description
Debug memory leaks, profile memory usage, optimize allocations. Use when heap grows unexpectedly, OOM errors occur, allocation performance matters, profiling shows memory bottleneck, Valgrind/ASAN reports issues, or Python objects not being collected. Covers C++ (Valgrind, ASAN, RAII), Python (tracemalloc, objgraph), and general patterns.
compatibility
"C++ requires Valgrind (Linux) or AddressSanitizer (Clang/GCC). Python uses tracemalloc (built-in) or objgraph (pip install)."
Memory Management Optimization
Persona: Systems programmer who treats memory as a finite resource - every allocation has a cost, every leak is unacceptable.
Process
Baseline - Measure current memory usage and allocation patterns
Identify - Find leaks, excessive allocations, or fragmentation
Analyze - Understand ownership, lifetimes, and allocation sites
Fix - Apply appropriate solution for the issue type
# Compile with ASAN
clang++ -fsanitize=address -g program.cpp
# Also useful:
-fsanitize=leak # Leak detection only
-fsanitize=memory # Uninitialized reads (Clang)
-fsanitize=undefined # UB detection
RAII Patterns
// BAD: Manual memory management
void bad() {
int* p = new int[100];
if (error) return; // LEAK
delete[] p;
}
// GOOD: RAII with smart pointers
void good() {
auto p = std::make_unique<int[]>(100);
if (error) return; // Automatic cleanup
}
Ownership
Use
unique_ptr
Single owner, no sharing
shared_ptr
Multiple owners
weak_ptr
Observer, breaks cycles
Raw pointer
Non-owning reference only
Qt-Specific
// Parent-child ownership
auto* child = new QWidget(parent); // parent deletes child
// deleteLater for event loop safety
obj->deleteLater();
// Watch for:
// - Deleting QObject during signal handling
// - Objects without parents in long-lived containers
Python Memory Debugging
tracemalloc (Built-in)
import tracemalloc
tracemalloc.start()
# ... code to profile ...
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats[:10]:
print(stat)
objgraph (Reference cycles)
import objgraph
# Find what's keeping objects alive
objgraph.show_backrefs(obj, max_depth=3)
# Find objects by type
objgraph.by_type('MyClass')
# Show growth between snapshots
objgraph.show_growth()
Common Python Leaks
Pattern
Fix
Circular references
weakref, break cycle
Global caches
Bounded cache, @lru_cache(maxsize=N)
Closures capturing
Copy values, use weakref
Event handlers
disconnect(), weak callbacks
Thread-local storage
Clean up on thread exit
General Optimization Patterns
Object Pooling
// Reuse objects instead of allocate/free
class ObjectPool {
std::vector<Object*> available;
public:
Object* acquire() {
if (available.empty())
return new Object();
auto* obj = available.back();
available.pop_back();
return obj;
}
void release(Object* obj) {
obj->reset();
available.push_back(obj);
}
};