| doc_id | NET-PERF-003 |
|---|---|
| doc_title | Memory Profiling Guide |
| doc_version | 1.0.0 |
| doc_date | 2026-04-04 |
| doc_status | Released |
| project | network_system |
| category | PERF |
SSOT: This document is the single source of truth for Memory Profiling Guide.
This guide explains how to profile memory usage in the Network System library and detect memory leaks.
# Build the memory profiling demo
cmake --build build --target network_memory_profile_demo
# Run all profiling scenarios
./build/bin/network_memory_profile_demo all
# Run specific scenario
./build/bin/network_memory_profile_demo connections
./build/bin/network_memory_profile_demo messages
./build/bin/network_memory_profile_demo stability| Component | Memory |
|---|---|
| TCP Socket | ~1 KB |
| Session State | ~2 KB |
| Buffers (default) | ~8 KB |
| Total | ~11 KB |
| Connections | Expected RSS |
|---|---|
| 0 | ~5 MB |
| 100 | ~6 MB |
| 1,000 | ~15 MB |
| 10,000 | ~115 MB |
Note: Actual values may vary by platform and configuration.
Best for: Detecting memory corruption, buffer overflows, use-after-free.
# Build with ASAN
cmake -B build_asan -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"
cmake --build build_asan
# Run with leak detection
ASAN_OPTIONS="detect_leaks=1" ./build_asan/bin/network_memory_profile_demo allOr use the provided script:
./scripts/run_with_asan.shBest for: Detecting data races and threading issues.
# Build with TSAN
cmake -B build_tsan -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=thread" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread"
cmake --build build_tsan
# Run
./build_tsan/bin/network_memory_profile_demo allBest for: Detailed leak detection, memory access analysis.
valgrind --leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
./build/bin/network_memory_profile_demo connectionsBest for: Tracking allocations over time, finding allocation hotspots.
heaptrack ./build/bin/network_memory_profile_demo messages
heaptrack_gui heaptrack.network_memory_profile_demo.*.gzBest for: macOS-specific profiling with GUI.
# Leaks template
xcrun xctrace record --template "Leaks" \
--launch -- ./build/bin/network_memory_profile_demo all
# Allocations template
xcrun xctrace record --template "Allocations" \
--launch -- ./build/bin/network_memory_profile_demo connectionsSymptoms: RSS increases continuously during operation.
Diagnosis:
./build/bin/network_memory_profile_demo stabilityCommon causes:
- Session cleanup not happening
- Message queue growing unbounded
- Connection pool not releasing
Solutions:
- Ensure proper cleanup on disconnect
- Set message queue limits
- Configure connection pool timeout
Symptoms: Memory usage much higher than expected per connection.
Diagnosis:
./build/bin/network_memory_profile_demo connectionsSolutions:
- Reduce buffer sizes
- Enable buffer pooling
- Check for retained references
Symptoms: Memory remains high after connections close.
Diagnosis:
# Run ASAN
ASAN_OPTIONS="detect_leaks=1" ./build_asan/bin/your_programSolutions:
- Fix any reported leaks
- Check shared_ptr cycles
- Verify cleanup order
Measures memory overhead per connection:
// Creates many connections and measures:
// - Memory before/after each batch
// - Per-connection overhead
// - Cleanup effectiveness
./build/bin/network_memory_profile_demo connectionsMeasures memory during message transmission:
// Sends many messages and measures:
// - Memory growth during transmission
// - Queue buffering overhead
// - Processing cleanup
./build/bin/network_memory_profile_demo messagesDetects memory leaks over time:
// Repeatedly connects/disconnects and measures:
// - Memory drift over time
// - Peak memory usage
// - Leak indicators
./build/bin/network_memory_profile_demo stability- Regular profiling: Run memory profiling in CI pipelines
- Baseline tracking: Compare against expected values after changes
- Stress testing: Profile under high load conditions
- Leak checking: Run ASAN/Valgrind before releases
- Platform testing: Memory behavior varies by OS
Add memory profiling to your CI pipeline:
# Example GitHub Actions step
- name: Memory Profile
run: |
cmake -B build_asan -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=address"
cmake --build build_asan --target network_memory_profile_demo
ASAN_OPTIONS="detect_leaks=1:halt_on_error=1" \
./build_asan/bin/network_memory_profile_demo stability- Uses
mach_task_basic_infofor memory stats - RSS reported in bytes
- Instruments provides best GUI experience
- Uses
/proc/self/statmfor memory stats - Valgrind and Heaptrack available
- RSS in pages (multiply by page size)
- Use Visual Studio Diagnostic Tools
- Consider DrMemory for leak detection
- Memory stats via Windows API
Memory delta from baseline: 50 KB
Memory leak indicator: OK
Memory delta from baseline: 5000 KB
Memory leak indicator: POSSIBLE LEAK
When you see potential issues:
- Run with ASAN to find specific leaks
- Use Valgrind for detailed analysis
- Check recent code changes
- Review cleanup code paths