mirror of
https://github.com/agessaman/MeshCore.git
synced 2026-07-12 13:08:48 +00:00
46041130b6
- Added `getQueueSize` method to `MyMesh` and `CommonCLI` for better queue management. - Introduced memory logging functionality in `MQTTBridge` to monitor heap usage and detect potential memory leaks. - Adjusted maximum queue size in `MQTTBridge` from 50 to 10 for improved resource management. - Enhanced command handling in `CommonCLI` to report memory status upon request.
5.0 KiB
5.0 KiB
MeshCore Memory Monitoring Guide
Quick Start
1. Find Your Device Port
# Linux/macOS
ls /dev/tty* | grep -E "(USB|ACM)"
# Common ports:
# /dev/ttyUSB0 - Linux USB serial
# /dev/ttyACM0 - Linux USB CDC
# /dev/cu.usbserial-* - macOS USB serial
# /dev/cu.usbmodem* - macOS USB CDC
2. Run Monitoring
# Monitor for 4 hours (default)
python3 monitor_memory.py /dev/ttyUSB0
# Monitor for 24 hours
python3 monitor_memory.py /dev/ttyUSB0 24
# Monitor for 2 hours with 60-second intervals
python3 monitor_memory.py /dev/ttyUSB0 2 --interval 60
What It Monitors
Memory Metrics
- Free Heap: Available memory in bytes
- Min Heap: Minimum free heap since boot
- Max Alloc: Largest allocatable block
- Queue Size: Number of queued MQTT packets
Calculated Metrics
- Heap Usage %: Percentage of total memory used
- Fragmentation %: How fragmented the heap is
Automatic Alerts
- LOW_MEMORY: Free heap < 50KB
- HIGH_FRAGMENTATION: Fragmentation > 50%
- QUEUE_BUILDUP: Queue size > 20 packets
- POSSIBLE_LEAK: Memory decreasing over time
Output Files
Console Output
[ 30.0m] Free: 102796, Min: 83544, Max: 75764, Queue: 0, Usage: 68.6%, Frag: 26.3%
[ 60.0m] Free: 101234, Min: 82345, Max: 74321, Queue: 2, Usage: 69.1%, Frag: 26.5%
⚠️ WARNING: HIGH_FRAGMENTATION
CSV Log File
Timestamp,Elapsed_Minutes,Free_Heap,Min_Heap,Max_Alloc,Queue_Size,Heap_Usage_Percent,Fragmentation_Percent
2024-01-15T10:30:00,0.0,102796,83544,75764,0,68.6,26.3
2024-01-15T11:00:00,30.0,101234,82345,74321,2,69.1,26.5
Understanding Results
Healthy System
- Free Heap: 150KB+ (stable)
- Min Heap: 120KB+ (stable)
- Max Alloc: 100KB+ (stable)
- Fragmentation: < 30%
- Queue: 0-10 packets
Warning Signs
- Free Heap: < 100KB or decreasing
- Min Heap: < 80KB or decreasing
- Fragmentation: > 50%
- Queue: > 20 packets consistently
Memory Leak Indicators
- Consistent decrease in Free Heap over time
- Min Heap dropping below previous minimums
- Max Alloc shrinking (fragmentation increasing)
- POSSIBLE_LEAK alert triggered
Long-Term Monitoring
24-Hour Test
python3 monitor_memory.py /dev/ttyUSB0 24
- Tests for memory leaks over extended period
- Monitors system stability under normal load
- Identifies gradual memory degradation
48-Hour Stress Test
python3 monitor_memory.py /dev/ttyUSB0 48 --interval 60
- Extended monitoring for critical deployments
- 60-second intervals reduce log file size
- Tests system under continuous operation
Troubleshooting
Device Not Responding
- Check port is correct:
ls /dev/tty* - Ensure device is connected and powered
- Try different baud rate if needed
- Check device is in correct mode
No Data in CSV
- Verify device responds to
memorycommand manually - Check serial connection is stable
- Ensure device has MQTT bridge enabled
High Memory Usage
- Check if it's stable or increasing
- Look for memory leak patterns
- Monitor queue size for packet buildup
- Consider reducing debug logging
Analysis Tools
Plot Memory Usage
import pandas as pd
import matplotlib.pyplot as plt
# Load CSV data
df = pd.read_csv('memory_monitor_20240115_103000.csv')
# Plot memory over time
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.plot(df['Elapsed_Minutes'], df['Free_Heap'])
plt.title('Free Heap Over Time')
plt.ylabel('Bytes')
plt.subplot(2, 2, 2)
plt.plot(df['Elapsed_Minutes'], df['Heap_Usage_Percent'])
plt.title('Heap Usage Percentage')
plt.ylabel('%')
plt.subplot(2, 2, 3)
plt.plot(df['Elapsed_Minutes'], df['Fragmentation_Percent'])
plt.title('Heap Fragmentation')
plt.ylabel('%')
plt.subplot(2, 2, 4)
plt.plot(df['Elapsed_Minutes'], df['Queue_Size'])
plt.title('Queue Size')
plt.ylabel('Packets')
plt.tight_layout()
plt.savefig('memory_analysis.png')
plt.show()
Check for Trends
# Calculate memory trend
df['Free_Heap_Trend'] = df['Free_Heap'].rolling(window=10).mean()
df['Trend_Slope'] = df['Free_Heap_Trend'].diff()
# Identify decreasing trends
decreasing = df[df['Trend_Slope'] < -1000]
if not decreasing.empty:
print("Memory decreasing trend detected!")
print(decreasing[['Elapsed_Minutes', 'Free_Heap', 'Trend_Slope']])
Best Practices
- Start with 4-hour baseline to establish normal patterns
- Monitor during peak usage times for worst-case scenarios
- Run 24-hour tests before production deployment
- Check logs regularly for warning signs
- Keep historical data for trend analysis
- Test after code changes to verify fixes
Emergency Procedures
If Memory Leak Detected
- Stop monitoring (Ctrl+C)
- Check recent code changes
- Look for unfreed allocations
- Test with reduced functionality
- Deploy memory leak fix
If System Crashes
- Check last known good memory values
- Identify crash threshold
- Add more frequent monitoring
- Implement memory safeguards
- Consider hardware upgrade