# lwIP：pbuf_alloc失败：PBUF_POOL耗尽，总计16，空闲0，最低0

- **ID:** `embedded/lwip-pbuf-pool-exhaustion`
- **领域:** embedded
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

lwIP的PBUF_POOL内存池为空，因为所有缓冲区都在使用中，通常由于网络流量峰值、未处理数据包的内存泄漏或池大小配置不足引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| lwIP 2.1.3 | active | — | — |
| lwIP 2.2.0 | active | — | — |
| STM32Cube_FW_H7_V1.11.0 | active | — | — |

## 解决方案

1. ```
   Increase PBUF_POOL_SIZE in lwipopts.h (e.g., from 16 to 32 or 64) to handle burst traffic, and reduce PBUF_POOL_BUFSIZE if packet sizes are small.
   ```
2. ```
   Add a check in the Ethernet ISR to drop packets if pool is low, preventing system lockup: `if (pbuf_alloc(PBUF_RAW, 0, PBUF_POOL) == NULL) { return ERR_MEM; }`
   ```
3. ```
   Implement a periodic task to free orphaned pbufs by checking `lwip_stats.mem.used` and calling `tcpip_thread()` processing to flush queues.
   ```

## 无效尝试

- **** — Larger windows require more buffers; if the pool size isn't increased, exhaustion still occurs under load. (80% 失败率)
- **** — This shifts computation to CPU but doesn't affect buffer allocation; pool exhaustion persists. (90% 失败率)
- **** — ARP cache management doesn't free PBUF_POOL buffers; those are for packet data, not ARP entries. (95% 失败率)
