Python 的内存分配策略
arena
arena: 多个pool聚合的结果
arena size
pool的大小默认值位4KB
arena的大小默认值256KB, 能放置 256/4=64 个pool
obmalloc.c
中代码
1 |
#define ARENA_SIZE (256 << 10) /* 256KB */ |
arena 结构
一个完整的arena = arena_object + pool集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
typedef uchar block; /* Record keeping for arenas. */ struct arena_object { /* The address of the arena, as returned by malloc. Note that 0 * will never be returned by a successful malloc, and is used * here to mark an arena_object that doesn't correspond to an * allocated arena. */ uptr address; /* Pool-aligned pointer to the next pool to be carved off. */ block* pool_address; /* The number of available pools in the arena: free pools + never- * allocated pools. */ uint nfreepools; /* The total number of pools in the arena, whether or not available. */ uint ntotalpools; /* Singly-linked list of available pools. */ // 单链表, 可用pool集合 struct pool_header* freepools; /* Whenever this arena_object is not associated with an allocated * arena, the nextarena member is used to link all unassociated * arena_objects in the singly-linked `unused_arena_objects` list. * The prevarena member is unused in this case. * * When this arena_object is associated with an allocated arena * with at least one available pool, both members are used in the * doubly-linked `usable_arenas` list, which is maintained in * increasing order of `nfreepools` values. * * Else this arena_object is associated with an allocated arena * all of whose pools are in use. `nextarena` and `prevarena` * are both meaningless in this case. */ // arena链表 struct arena_object* nextarena; struct arena_object* prevarena; }; |
arena_object的作用
1 2 3 |
1. 与其他arena连接, 组成双向链表 2. 维护arena中可用的pool, 单链表 3. 其他信息 |
pool_header
与 arena_object
1 2 |
pool_header和管理的blocks内存是一块连续的内存 => pool_header被申请时, 其管理的block集合的内存一并被申请 arena_object和其管理的内存是分离的 => arena_object被申请时, 其管理的pool集合的内存没有被申请, 而是在某一时刻建立的联系 |
arena的两种状态
arena存在两种状态: 未使用(没有建立联系)/可用(建立了联系)
全局由两个链表维护着
1 2 3 4 5 6 7 8 9 10 11 |
/* The head of the singly-linked, NULL-terminated list of available * arena_objects. */ // 单链表 static struct arena_object* unused_arena_objects = NULL; /* The head of the doubly-linked, NULL-terminated at each end, list of * arena_objects associated with arenas that have pools available. */ // 双向链表 static struct arena_object* usable_arenas = NULL; |
arena的初始化
首先, 来看下初始化相关的一些参数定义
代码obmalloc.c
ϯ,禁止转载!
欢迎加入伯乐在线 专栏作者。
欢迎加入伯乐在线 专栏作者。
Python 的内存分配策略
arena
arena: 多个pool聚合的结果
arena size
pool的大小默认值位4KB
arena的大小默认值256KB, 能放置 256/4=64 个pool
obmalloc.c
中代码
1 |
#define ARENA_SIZE (256 << 10) /* 256KB */ |
arena 结构
一个完整的arena = arena_object + pool集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
typedef uchar block; /* Record keeping for arenas. */ struct arena_object { /* The address of the arena, as returned by malloc. Note that 0 * will never be returned by a successful malloc, and is used * here to mark an arena_object that doesn't correspond to an * allocated arena. */ uptr address; /* Pool-aligned pointer to the next pool to be carved off. */ block* pool_address; /* The number of available pools in the arena: free pools + never- * allocated pools. */ uint nfreepools; /* The total number of pools in the arena, whether or not available. */ uint ntotalpools; /* Singly-linked list of available pools. */ // 单链表, 可用pool集合 struct pool_header* freepools; /* Whenever this arena_object is not associated with an allocated * arena, the nextarena member is used to link all unassociated * arena_objects in the singly-linked `unused_arena_objects` list. * The prevarena member is unused in this case. * * When this arena_object is associated with an allocated arena * with at least one available pool, both members are used in the * doubly-linked `usable_arenas` list, which is maintained in * increasing order of `nfreepools` values. * * Else this arena_object is associated with an allocated arena * all of whose pools are in use. `nextarena` and `prevarena` * are both meaningless in this case. */ // arena链表 struct arena_object* nextarena; struct arena_object* prevarena; }; |
arena_object的作用
1 2 3 |
1. 与其他arena连接, 组成双向链表 2. 维护arena中可用的pool, 单链表 3. 其他信息 |
pool_header
与 arena_object
1 2 |
pool_header和管理的blocks内存是一块连续的内存 => pool_header被申请时, 其管理的block集合的内存一并被申请 arena_object和其管理的内存是分离的 => arena_object被申请时, 其管理的pool集合的内存没有被申请, 而是在某一时刻建立的联系 |
arena的两种状态
arena存在两种状态: 未使用(没有建立联系)/可用(建立了联系)
全局由两个链表维护着
1 2 3 4 5 6 7 8 9 10 11 |
/* The head of the singly-linked, NULL-terminated list of available * arena_objects. */ // 单链表 static struct arena_object* unused_arena_objects = NULL; /* The head of the doubly-linked, NULL-terminated at each end, list of * arena_objects associated with arenas that have pools available. */ // 双向链表 static struct arena_object* usable_arenas = NULL; |
arena的初始化
首先, 来看下初始化相关的一些参数定义
代码obmalloc.c