Skip to content

Commit 83aa90a

Browse files
feat: 添加PSRAM内存优化和动态栈大小管理
主要改进: - 新增memory_utils.h工具库,提供PSRAM感知的内存分配 - 优化任务栈大小配置,根据PSRAM可用性动态调整 - 新增Kconfig选项用于PSRAM使用和重连接栈大小配置 - 减少默认栈大小:worker 6KB->4KB, scheduler 6KB->4KB, callback 6KB->5KB - 重连接任务栈大小增加到12KB(无PSRAM)或16KB(有PSRAM)以防止栈溢出 - 连接超时从10秒增加到15秒以提高重连成功率 - 最大并发回调任务从3减少到2以节省内存 - 消息队列大小从50减少到20以节省内存 - 改进日志输出,添加内存统计和栈使用监控 - 优化websocket传输层的锁获取重试机制 性能提升: - 使用移动语义减少字符串拷贝开销 - 大型缓冲区优先使用PSRAM以减轻内部RAM压力 - 更积极的缓冲区收缩策略释放未使用内存 关键修复: - 修复重连接过程中的栈溢出问题 - 改进异常处理和错误恢复机制 - 增强并发场景下的线程安全性
1 parent 482a620 commit 83aa90a

File tree

6 files changed

+666
-97
lines changed

6 files changed

+666
-97
lines changed

Kconfig

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
menu "ESP32 SignalR Client Configuration"
22

3+
config SIGNALR_USE_PSRAM
4+
bool "Use PSRAM for large buffers when available"
5+
default y
6+
depends on SPIRAM
7+
help
8+
When enabled and PSRAM is available, large buffers (message queue,
9+
receive buffer, etc.) will be allocated in PSRAM to reduce internal
10+
RAM pressure. This can significantly reduce stack overflow issues.
11+
Recommended: Enable if your ESP32 has PSRAM.
12+
313
config SIGNALR_WORKER_POOL_SIZE
414
int "Worker thread pool size"
515
default 2
@@ -11,36 +21,69 @@ menu "ESP32 SignalR Client Configuration"
1121

1222
config SIGNALR_WORKER_STACK_SIZE
1323
int "Worker task stack size (bytes)"
14-
default 6144
24+
default 4096
1525
range 2048 8192
1626
help
1727
Stack size for each worker task in bytes.
18-
CRITICAL: C++ exception handling requires minimum 4-5KB stack!
19-
Default: 6144 (6KB) - required for safe exception unwinding
20-
Old default 3KB caused stack overflow during send() failures with exceptions.
21-
DO NOT reduce below 4096 unless you disable all exception throwing.
28+
OPTIMIZED: Reduced from 6KB to 4KB - sufficient with PSRAM offloading.
29+
With PSRAM enabled, the library dynamically adjusts stack sizes.
30+
Default: 4096 (4KB)
2231
Enable CONFIG_SIGNALR_ENABLE_STACK_MONITORING to measure actual usage.
2332

2433
config SIGNALR_SCHEDULER_STACK_SIZE
2534
int "Scheduler task stack size (bytes)"
26-
default 6144
27-
range 4096 16384
35+
default 4096
36+
range 3072 16384
2837
help
2938
Stack size for the scheduler task in bytes.
30-
Default: 6144 (6KB, optimized for ESP32)
31-
Recommended: Keep at 6KB unless stack monitoring shows otherwise.
39+
OPTIMIZED: Reduced from 6KB to 4KB - scheduler is lightweight.
40+
Default: 4096 (4KB)
3241
Enable CONFIG_SIGNALR_ENABLE_STACK_MONITORING to measure actual usage.
3342

3443
config SIGNALR_CALLBACK_STACK_SIZE
3544
int "WebSocket callback task stack size (bytes)"
36-
default 6144
45+
default 5120
3746
range 4096 32768
3847
help
3948
Stack size for the WebSocket callback processing task.
40-
Default: 6144 (6KB, reduced from 32KB for memory efficiency)
41-
Recommended: 6KB for typical SignalR usage, 8KB for complex JSON processing
49+
OPTIMIZED: Reduced from 6KB to 5KB for memory efficiency.
4250
Increase if you experience stack overflow in callbacks.
4351
Enable CONFIG_SIGNALR_ENABLE_STACK_MONITORING to measure actual usage.
52+
53+
config SIGNALR_RECONNECT_STACK_SIZE
54+
int "Reconnect task stack size (bytes)"
55+
default 12288
56+
range 10240 32768
57+
help
58+
Stack size for the reconnection task.
59+
CRITICAL: This task runs the entire connection flow synchronously:
60+
- WebSocket client creation
61+
- SSL/TLS handshake (if using wss://)
62+
- SignalR handshake with JSON parsing
63+
- C++ exception handling with deep call chains
64+
Default: 12288 (12KB) - minimum required!
65+
Use 16384 (16KB) if using SSL/TLS or complex JSON.
66+
DO NOT reduce below 10KB or stack overflow WILL occur!
67+
68+
config SIGNALR_CONNECTION_TIMEOUT_MS
69+
int "WebSocket connection timeout (milliseconds)"
70+
default 15000
71+
range 5000 60000
72+
help
73+
Timeout for establishing WebSocket connection.
74+
OPTIMIZED: Increased from 10s to 15s for better reconnection success.
75+
During server restart or network recovery, connections may take longer.
76+
Default: 15000 (15 seconds)
77+
78+
config SIGNALR_MAX_CALLBACK_TASKS
79+
int "Maximum concurrent callback execution tasks"
80+
default 2
81+
range 1 5
82+
help
83+
Maximum number of concurrent tasks for executing SignalR callbacks.
84+
OPTIMIZED: Reduced from 3 to 2 to save ~5KB per task.
85+
Lower values reduce memory usage but may slow callback processing.
86+
Default: 2
4487

4588
config SIGNALR_MAX_MESSAGE_SIZE
4689
int "Maximum message size (bytes)"
@@ -91,13 +134,13 @@ menu "ESP32 SignalR Client Configuration"
91134

92135
config SIGNALR_MAX_QUEUE_SIZE
93136
int "Maximum message queue size"
94-
default 50
95-
range 10 200
137+
default 20
138+
range 5 100
96139
help
97140
Maximum number of messages that can be queued before dropping oldest messages.
98-
Prevents memory leak if message processing is slower than message arrival rate.
99-
Default: 50 messages
100-
Recommendation: Set based on your message rate and processing speed.
141+
OPTIMIZED: Reduced from 50 to 20 to save memory.
142+
Each queued message consumes heap memory.
143+
Default: 20 messages
101144

102145
config SIGNALR_ENABLE_STACK_MONITORING
103146
bool "Enable stack usage monitoring"

0 commit comments

Comments
 (0)