MEM_ERR_MEM embedded resource_error ai_generated true

lwIP: memp_malloc failed: PBUF_POOL exhausted

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

Also available as: JSON · Markdown · 中文
81%Fix Rate
86%Confidence
1Evidence
2024-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
lwIP v2.1.3 active
FreeRTOS v10.4.6 active
STM32Cube_FW_F7 v1.17.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 85% success Monitor pbuf pool usage with a periodic task: call memp_stats() and log free pbuf count; if below threshold, force free old connections via tcp_abort().
    Monitor pbuf pool usage with a periodic task: call memp_stats() and log free pbuf count; if below threshold, force free old connections via tcp_abort().
  2. 80% success Increase PBUF_POOL_SIZE and PBUF_POOL_BUFSIZE in lwipopts.h: set PBUF_POOL_SIZE to 50 and PBUF_POOL_BUFSIZE to 1524 to handle maximum Ethernet frames.
    Increase PBUF_POOL_SIZE and PBUF_POOL_BUFSIZE in lwipopts.h: set PBUF_POOL_SIZE to 50 and PBUF_POOL_BUFSIZE to 1524 to handle maximum Ethernet frames.
  3. 75% success Implement a pbuf leak detection: wrap pbuf_alloc() and pbuf_free() with counters, and log stack traces when free count drops below 10% of pool size.
    Implement a pbuf leak detection: wrap pbuf_alloc() and pbuf_free() with counters, and log stack traces when free count drops below 10% of pool size.

中文步骤

  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% 时记录堆栈跟踪。

Dead Ends

Common approaches that don't work:

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

    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% fail

    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% fail

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