# lwIP: memp_malloc failed: PBUF_POOL exhausted

- **ID:** `embedded/lwip-memp-pbuf-pool-exhausted`
- **Domain:** embedded
- **Category:** resource_error
- **Error Code:** `MEM_ERR_MEM`
- **Verification:** ai_generated
- **Fix Rate:** 81%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| lwIP v2.1.3 | active | — | — |
| FreeRTOS v10.4.6 | active | — | — |
| STM32Cube_FW_F7 v1.17.0 | active | — | — |

## Workarounds

1. **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().** (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().
   ```
2. **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.** (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.
   ```
3. **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.** (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.
   ```

## Dead Ends

- **Increase PBUF_POOL_SIZE to a very large value (e.g., 1000)** — Large pools consume excessive RAM; memory exhaustion may cause system instability without fixing the leak. (70% fail)
- **Disable TCP window scaling to reduce buffer usage** — TCP performance degrades significantly, and the issue may persist if the leak is in UDP or raw packets. (85% fail)
- **Use dynamic pbuf allocation (PBUF_RAM) instead of pool** — Dynamic allocation can fragment heap and cause out-of-memory errors, especially in constrained systems. (80% fail)
