scudo基础学习

Scudo 是一个动态内存分配器,优势在于安全性,可以检测到一些内存漏洞(eap-overflow,double-free,use-after-free),但牺牲了一些性能

从Android11开始,Scudo负责所有用户空间native(c,c++)内存分配和释放:

(备注:

  1. java/kotlin层常用bump allocator、buddy allocator分配内存
  2. kernel主要采用slab和boot memory allocator

Linux Memory -- 三大allocator原理_offsetallocator-CSDN博客

Scudo 定义了 Primary 和 Secondary 两种类型分配器:

  • Primary Allocator :小于64 k时使用 Primary Allocator (分配小内存
  • Secondary Allocator:大于 64 k 使用 Secondary Allocator (分配大内存 ), 使用mmap分配

Scudo初始化之后会预先分配33个内存区域( SizeClass:一个region )的虚拟内存,0号region 用于存放内存管理元数据,每个 SizeClass 提供大小X的内存分配,每个 SizeClass mmap 256M的虚拟内存,分成大小相同的

多块内存。

SizeClass最小是32 Bytes,依次48 Bytes ,64 Bytes ... ,定义如下 ./external/scudo/standalone/primary64.h:

复制代码
static constexpr u32 Classes[] = {
      0x00020, 0x00030, 0x00040, 0x00050, 0x00060, 0x00070, 0x00090, 0x000b0,
      0x000c0, 0x000e0, 0x00120, 0x00160, 0x001c0, 0x00250, 0x00320, 0x00450,
      0x00670, 0x00830, 0x00a10, 0x00c30, 0x01010, 0x01210, 0x01bd0, 0x02210,
      0x02d90, 0x03790, 0x04010, 0x04810, 0x05a10, 0x07310, 0x08210, 0x10010,
  };

分配规则: 申请的内存大小从小到大选择合适的Region:先从32 Bytes 开始,如果不能分配就从48 Bytes ,64 Bytes......65552Bytes(64kb)

关键log:

某个region (SizeClass)内存申请完了(超过256M),系统会提示Scudo OOM: The process has exhausted 256M for size class 并且打印出每个SizeClass使用现状,如下所示:

scudo : Stats: SizeClassAllocator64: 8053M mapped (0M rss) in 8370721712 allocations; remains 32047130

有些误解:

  • 每个进程申请的虚拟内存大小是固定的?

❌,没有限制(javaHeap 有限制,NativeHeap 没有限制,每个进程的vma个数vm.max_map_count )是有限制的),可以通过命令查到 sysctl -a|grep vm.max_map_count

大多数进程只用32个vma,一般不会超过。1个SizeClass就是一个vma,但vma的虚拟内存大小是没有限制的 ,所以整体可以理解成无限制的,本地demo实验证明 22G 车机上能分配到16G native内存。

  • 每个Scudo分配的内存上限是固定的?

❌,Primary Allocator(SizeClassAllocator64)申请的内存是有限的,Secondary Allocator申请的内存是无限的

Primary Allocator能分配的最大内存为8G(32*256M),如果分配完了,再申请64kb下的内存会报错Scudo ERROR: internal map failure (NO MEMORY) requesting

Secondary Allocator内部有的Cache最多缓存 32 个不超过 2M 的 VMA(32*2M),但分配的总量是无限制的,只有系统内存不足时才会分配失败

  • Scudo分配失败一定是系统内存不足?

❌,如果Primary Allocator满了,申请小内存也会失败

TSD(Thread Specific Data)

为了解决多线程下内存的分配加锁导致的性能问题,为每个线程分配一块专有的内存(TSD),由于Android 系统 TSD 的数量有限 ,默认有2个 TSD ,采用负载均衡算法实现同步

错误排查:

scudo : Scudo ERROR: internal map failure (NO MEMORY) requesting

ibc : pthread_create failed: couldn't allocate'

ibc :malloc(4096) failed: returning null pointer

原因分析:map发生快速内存泄漏,导致分配器分配不出更多内存

参考文献:

https://zhuanlan.zhihu.com/p/235620563
https://zhuanlan.zhihu.com/p/560188790
https://zhuanlan.zhihu.com/p/423062509
Android内存分配-Scudo内存分配器 - 简书
https://zhuanlan.zhihu.com/p/604315376
https://zhuanlan.zhihu.com/p/644169143
Scudo | Android Open Source Project

相关推荐
UWA2 天前
资源回收“两头堵”:RUUA调了会卡,不调内存又涨
性能优化·memory·游戏开发
weigangwin2 天前
采用 mem0 之前,先决定 Agent 到底允许记住什么
人工智能·opencv·ai·llm·memory·ai agent·mem0
阿拉斯攀登3 天前
Prompt 工程与答案生成优化
prompt·agent·memory·知识库·向量数据库·rag
阿拉斯攀登4 天前
安全与可控性:输出校验、权限控制
人工智能·chatgpt·agent·memory·claude·知识库·向量数据库
尽兴-6 天前
Redis 为什么快?
数据库·redis·内存
knqiufan15 天前
自进化的 Agent 记忆层 —— PowerMem 简易操作手册
memory·agentic·powermem
knqiufan16 天前
从 Python 到 TypeScript,用 GLM-5.2 跑通 PowerMem SDK 的长程任务工程
ai·memory·agentic·powermem
Artech21 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
cup1124 天前
[开源] Memory Checker:极致轻量的 Windows 托盘内存监测工具,告别内存焦虑
python·内存·工具·任务管理器·托盘
H Journey1 个月前
汇编基础知识:地址总线、数据总线、内存地址空间、物理内存核心知识梳理
内存·cpu·总线