MEM_ERR_MEM embedded resource_error ai_generated true

lwIP:memp_malloc 失败:PBUF_POOL 耗尽

lwIP: memp_malloc failed: PBUF_POOL exhausted

ID: embedded/lwip-memp-pbuf-pool-exhausted

其他格式: JSON · Markdown 中文 · English
81%修复率
86%置信度
1证据数
2024-06-01首次发现

版本兼容性

版本状态引入弃用备注
lwIP v2.1.3 active
FreeRTOS v10.4.6 active
STM32Cube_FW_F7 v1.17.0 active

根因分析

lwIP pbuf 池(PBUF_POOL)由于网络流量峰值、数据包处理中的内存泄漏或 lwipopts.h 中池大小配置不足而耗尽空闲缓冲区。

English

The lwIP pbuf pool (PBUF_POOL) ran out of free buffers due to network traffic spikes, memory leaks in packet processing, or insufficient pool size configuration in lwipopts.h.

generic

官方文档

https://www.nongnu.org/lwip/2_1_x/group__pbuf.html

解决方案

  1. 使用周期性任务监控 pbuf 池使用情况:调用 memp_stats() 并记录空闲 pbuf 数量;如果低于阈值,通过 tcp_abort() 强制释放旧连接。
  2. 在 lwipopts.h 中增加 PBUF_POOL_SIZE 和 PBUF_POOL_BUFSIZE:设置 PBUF_POOL_SIZE 为 50,PBUF_POOL_BUFSIZE 为 1524,以处理最大以太网帧。
  3. 实现 pbuf 泄漏检测:使用计数器包装 pbuf_alloc() 和 pbuf_free(),当空闲计数低于池大小的 10% 时记录堆栈跟踪。

无效尝试

常见但无效的做法:

  1. Increase PBUF_POOL_SIZE to a very large value (e.g., 1000) 70% 失败

    Large pools consume excessive RAM; memory exhaustion may cause system instability without fixing the leak.

  2. Disable TCP window scaling to reduce buffer usage 85% 失败

    TCP performance degrades significantly, and the issue may persist if the leak is in UDP or raw packets.

  3. Use dynamic pbuf allocation (PBUF_RAM) instead of pool 80% 失败

    Dynamic allocation can fragment heap and cause out-of-memory errors, especially in constrained systems.