解决 Memcached 报错 SERVER_ERROR out of memory storing object slab 内存浪费修复
排查 Memcached 内存尚未达到 -m 分配上限即频繁抛出 SERVER_ERROR out of memory storing object,分析 Slab Allocation 分配器机制、增长因子与 Chunk 浪费。
问题现象
向 Memcached 写入特定大小的对象时,服务端返回错误:
SERVER_ERROR out of memory storing object
STORED
SERVER_ERROR out of memory storing object
触发根因
- Memcached 采用 Slab 分配器预切分内存页(Page)为固定大小的 Chunk(Slab Class);
- 特定大小的 Slab 类别页面已耗尽,而其他 Slab 类别还有大量空闲内存,默认不支持跨类别借调;
- 默认增长因子
-f 1.25与对象尺寸分布不拟合,造成严重的内部内存碎片浪费。
解决步骤
启动 Memcached 时开启 slab 动态自动重平衡机制(Slab Automove)
修改启动参数:
memcached -m 4096 -u nobody -o slab_reassign,slab_automove=1
使用 memcached-tool 分析当前各 Slab 类的利用率
检测内存倾斜:
memcached-tool localhost:11211 display
手动触发 Slab 页面重新指派(Slab Reassign)救援耗尽类别
通过 telnet 或 nc 发送:
slabs reassign <SOURCE_SLAB_ID> <DEST_SLAB_ID>
验证确认
重新执行写入测试:
内存页面在各类尺寸间智能流动,内存利用率提升至 95% 以上,再无假性 OOM 发生。


