基本阅读完了, 只是没时间梳理, 趁着这今天时间比较空
逐步梳理, 发上来……也算是小结下, 要开始准备简历找工作了>_
这篇略长, 带很多图, 所以一分为二
Python的内存管理架构
基本分层
在Objects/obmalloc.c
源码中, 给了一个分层划分
1234567891011121314151617181920 | _____ ______ ______ ________ [ int ] [ dict ] [ list ] ... [ string ] Python core |+3 | <——– Object–specific memory ——-> | <— Non–object memory —> | _______________________________ | | [ Python\’s object allocator ] | |+2 | ####### Object memory ####### | <—— Internal buffers ——> | ______________________________________________________________ | [ Python\’s raw memory allocator (PyMem_ API) ] |+1 | <——– Python memory (under PyMem manager\’s control) ———> | | __________________________________________________________________ [ Underlying general–purpose allocator (ex: C library malloc) ] 0 | <——— Virtual memory allocated for the python process ———-> | ========================================================================= _______________________________________________________________________ [ OS–specific Virtual Memory Manager (VMM) ]–1 | <—– Kernel dynamic storage allocation & management (page–based) —-> | __________________________________ __________________________________ [ ] [ ]–2 | <— Physical memory: ROM/RAM —> | | <— Secondary storage (swap) —> | |
可以看到
1234567891011121314 | layer 3: Object–specific memory(int/dict/list/string....) Python 实现并维护 更高抽象层次的内存管理策略, 主要是各类特定对象的缓冲池机制. 具体见前面几篇涉及的内存分配机制 layer 2: Python\’s object allocator Python 实现并维护 实现了创建/销毁Python对象的接口(PyObject_New/Del), 涉及对象参数/引用计数等 layer 1: Python\’s raw memory allocator (PyMem_ API) Python 实现并维护, 包装了第0层的内存管理接口, 提供统一的raw memory管理接口 封装的原因: 不同操作系统 C 行为不一定一致, 保证可移植性, 相同语义相同行为 layer 0: Underlying general–purpose allocator (ex: C library malloc) 操作系统提供的内存管理接口, 由操作系统实现并管理, Python不能干涉这一层的行为 |
第三层layer 3
前面已经介绍过了, 几乎每种常用的数据类型都伴有一套缓冲池机制.
在这里, 我们关注的是layer 2/1
简要介绍下layer 1
, 然后重点关注layer 2
, 这才是重点
layer 1: PyMem_ API
PyMem_ API
是对操作系统内存管理接口进行的封装
查看pymem.h
可以看到
12345678910111213141516171819202122232425262728293031193-27\”>2728293031这篇略长, 带很多图, 所以一分为二
Python的内存管理架构基本分层在
可以看到
第三层 在这里, 我们关注的是 简要介绍下 layer 1: PyMem_ API
查看
|