Python源码阅读-内存管理机制(二)
admin
2023-07-31 00:36:56
0

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集合

1234567891011121314151617181920212223242526272829303132333435363738394041424344 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的作用

123 1. 与其他arena连接, 组成双向链表2. 维护arena中可用的pool, 单链表3. 其他信息

pool_headerarena_object

12 pool_header和管理的blocks内存是一块连续的内存 => pool_header被申请时, 其管理的block集合的内存一并被申请arena_object和其管理的内存是分离的 => arena_object被申请时, 其管理的pool集合的内存没有被申请, 而是在某一时刻建立的联系

arena的两种状态

arena存在两种状态: 未使用(没有建立联系)/可用(建立了联系)

全局由两个链表维护着

1234567891011 /* 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集合

1234567891011121314151617181920212223242526272829303132333435363738394041424344 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的作用

123 1. 与其他arena连接, 组成双向链表2. 维护arena中可用的pool, 单链表3. 其他信息

pool_headerarena_object

12 pool_header和管理的blocks内存是一块连续的内存 => pool_header被申请时, 其管理的block集合的内存一并被申请arena_object和其管理的内存是分离的 => arena_object被申请时, 其管理的pool集合的内存没有被申请, 而是在某一时刻建立的联系

arena的两种状态

arena存在两种状态: 未使用(没有建立联系)/可用(建立了联系)

全局由两个链表维护着

1234567891011 /* 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

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python查找阿姆斯特朗数 题目解释 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...