SlideShare a Scribd company logo
Python⾼级内存管理
- xiaorui.cc
Object-specific allocators
_____ ______ ______ ________
[ 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) --> |
* Request in bytes Size of allocated block Size class idx
* ----------------------------------------------------------------
* 1-8 8 0
* 9-16 16 1
* 17-24 24 2
* 25-32 32 3
* 33-40 40 4
* 41-48 48 5
* 49-56 56 6
* 57-64 64 7
* ... ... ...
* 497-504 504 62
* 505-512 512 63
*
*
*/
名词解释
process heap
Arenas
Pool
UsedPools
FreePools
method
posix malloc
python memory pool
object buffer pool
Arena
FeeePool
Pool
Pool
Pool
Pool
Headers
No BLock
Arena
malloc heap &
pool
Process
stack
heap
bss
init data
text
UserPool
1-8
… …
249 - 256
Pool
Free Block
Free Block
Use Block
userdpool design
UserdPools
1-8
9-16
17-24
…
…
249-256
Pool
Header
Free Block
Free Block
Header
分配
回收
…
Pool
Free Block
Free Block
Use Block
同⼀个Pool下Block⼀样长
单Pool为4kb
Block及Pool都为单链表
free pool desgin
FeeePool
Pool
Pool
Pool
…
Pool
Headers
No BLock
Pool
Headers
No BLock
Pool为4kb⼤小
Pool清理Headers
where store variable ?
run-time Stack
list
dict
int
heap
[1 ,2, 3]
{“n”: “1”}
1
why ?
In [1]: a = 123
In [2]: b = 123
In [3]: a is b
Out[3]: True
In [4]: a = 1000
In [5]: b = 1000
In [6]: a is b
Out[6]: False
In [7]: a = 'n'
In [8]: b = 'n'
In [9]: a is b
Out[9]: True
In [10]: a = "python"
In [11]: b = "python"
In [12]: a is b
Out[12]: True
why ?
In [1]: def go(var):
...: print id(var)
…:
In [2]: id(a)
Out[2]: 4401335072
In [3]: go(a)
4401335072
In [10]: a = b = 'nima'
In [11]: b = a
In [12]: a is b
Out[12]: True
In [13]: b = 'hehe'
In [14]: a is b
Out[14]: False
只有引用 ?
python objects stored in memory?
names
names
object
Python Has Names, Not Variables ! ! !
整数对象池
-5 -4 … 0 … 256 257
小整数 ⼤整数
… … -5 -4 … … 257 … …
var_1 var_2
… … -5 -4 … … 257 … …
var_3 var_4
the same addr !
not the same addr !
28 bytes
解释器初始化
整数对象池
Block List
Free List
PyIntBlock PyIntBlock
PyIntBlock PyIntBlock
不会归还给 Arena和os ! ! !
字符对象池
a b c d … … …
var_1 var_2
the same addr !
单个字符38 bytes
由解释器初始化
字符串对象池
aa en cao oh woyao buyao kuai feile
var_1
0 1 2 3 …
var_2
hash存储变量
共用地址
记录引用计数
ref
ref count
x = 300
y = x
z = [x, y]
X
ref += 1
300
y
Z
ref += 1
ref += 2
References -> 4 !
What does del do?
x = 300
y = x
del x
X
ref -= 1
300
y
References -> 1!The del statement doesn’t delete objects.
• removes that name as a reference to that object
• reduces the ref count by 1
ref count case
def go():
w = 300
go()
a = “fuc . ”
del a
b = “en, a”
b = None
ref count +1
w is out of scope; ref count -1
del a; ref count -1
重新赋值; ref count -1
cyclical refclass Node:
def __init__(self, va):
self.va = va
def next(self, next):
self.next = next
mid = Node(‘root’)
left = Node(‘left’)
right = Node(‘right’)
mid(left)
left.next(right)
right.next(left)
Mid
rightleft
if del mid node:
how ?
mark & sweep
gc root
b
a
w
c
K G
R
分代回收
node node node node node node node
node node node node node node
node node node node node node
可变 vs 不可变 (obj)
string
int
tuple
list
dict
container objects
a = [10, 10, 11]
b = a
PyListObject
Type list
rc 1
items
size
… …
PyObject
Type integer
rc 2
value 10
PyObject
Type integer
rc 1
value 11
10
10
11
copy.copy
a = [10, 10, [10, 11] ]
b = copy.copy(a)
PyListObject
Type list
rc 1
items
size
… …
PyObject
Type integer
rc 2
value 10
PyObject
Type integer
rc 1
value 11
10
10
ref
PyListObject
Type list
rc 1
items
size
… …
10
10
ref
PyListObject
10
11
copy.deepcopy
a = [10, [ 10, 11 ] ]
b = copy.deep(a)
PyListObject
Type list
rc 1
items
size
… …
PyObject
Type integer
rc 2
value 10
PyObject
Type integer
rc 1
value 11
10
ref
PyListObject
Type list
rc 1
items
size
… …
10
ref
PyListObject
10
11
PyListObject
10
11
diy gc
import gc
import sys
gc.set_debug(gc.DEBUG_STATS|gc.DEBUG_LEAK)
a=[]
b=[]
a.append(b)
print 'a refcount:',sys.getrefcount(a) # 2
print 'b refcount:',sys.getrefcount(b) # 3
del a
del b
print gc.collect() # 0
Garbage Collector Optimize
memory bound
可以降低threshold来时间换空间
cpu bound
提⾼threshold来空间换时间
暂停gc, 引⼊master worker设计
引用计数 跟 gil 的影响 ?
gc 是否是原⼦ ?
gc的 stop the world现象 ?
…
Q & A
“ END ”
– xiaorui.cc

More Related Content

PDF
Clustering com numpy e cython
PDF
Palestra sobre Collections com Python
PDF
Statistical computing 01
PPTX
Super Advanced Python –act1
PDF
Introducción a Elixir
DOCX
ggplot2 extensions-ggtree.
PPTX
R intro 20140716-advance
PDF
Useful javascript
Clustering com numpy e cython
Palestra sobre Collections com Python
Statistical computing 01
Super Advanced Python –act1
Introducción a Elixir
ggplot2 extensions-ggtree.
R intro 20140716-advance
Useful javascript

What's hot (20)

PDF
Groovy collection api
PPTX
Webinar: Replication and Replica Sets
PDF
Damn Fine CoffeeScript
PDF
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
PDF
Python For Data Science Cheat Sheet
DOCX
Advanced Data Visualization Examples with R-Part II
PPTX
Android Guava
PDF
Building Real Time Systems on MongoDB Using the Oplog at Stripe
PDF
Exploring slides
KEY
Haskellで学ぶ関数型言語
PDF
Elm: give it a try
PDF
집단지성 프로그래밍 08-가격모델링
PDF
밑바닥부터 시작하는 의료 AI
PDF
Python postgre sql a wonderful wedding
PPTX
Intoduction to dynamic memory allocation
PDF
Frsa
PDF
Pybelsberg — Constraint-based Programming in Python
PDF
Python seaborn cheat_sheet
PPTX
Introduzione a C#
Groovy collection api
Webinar: Replication and Replica Sets
Damn Fine CoffeeScript
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Python For Data Science Cheat Sheet
Advanced Data Visualization Examples with R-Part II
Android Guava
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Exploring slides
Haskellで学ぶ関数型言語
Elm: give it a try
집단지성 프로그래밍 08-가격모델링
밑바닥부터 시작하는 의료 AI
Python postgre sql a wonderful wedding
Intoduction to dynamic memory allocation
Frsa
Pybelsberg — Constraint-based Programming in Python
Python seaborn cheat_sheet
Introduzione a C#
Ad

Viewers also liked (6)

PPTX
cdn的那些事儿
PDF
Development with Qt for Windows CE
PPTX
领域驱动设计与模型驱动开发
PDF
Enterprise Architecture Implementation And The Open Group Architecture Framew...
PDF
Micro service
PDF
InnoDB Locking Explained with Stick Figures
cdn的那些事儿
Development with Qt for Windows CE
领域驱动设计与模型驱动开发
Enterprise Architecture Implementation And The Open Group Architecture Framew...
Micro service
InnoDB Locking Explained with Stick Figures
Ad

Similar to python高级内存管理 (20)

PDF
An overview of Python 2.7
PDF
A tour of Python
PDF
A Few of My Favorite (Python) Things
PDF
python-cheatsheets.pdf
PDF
python-cheatsheets that will be for coders
PPT
Profiling and optimization
PDF
Обзор фреймворка Twisted
PDF
Обзор фреймворка Twisted
PDF
Functional Programming inside OOP? It’s possible with Python
PDF
Introduction to Python
PDF
PHP 7 – What changed internally? (Forum PHP 2015)
PDF
PHP 7 – What changed internally? (PHP Barcelona 2015)
PPT
Python 101 language features and functional programming
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
PDF
Python fundamentals - basic | WeiYuan
PDF
Intro to Python
PPTX
Ggplot2 v3
PPT
python language programming presentation
PPTX
Replication and Replica Sets
PDF
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
An overview of Python 2.7
A tour of Python
A Few of My Favorite (Python) Things
python-cheatsheets.pdf
python-cheatsheets that will be for coders
Profiling and optimization
Обзор фреймворка Twisted
Обзор фреймворка Twisted
Functional Programming inside OOP? It’s possible with Python
Introduction to Python
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
Python 101 language features and functional programming
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Python fundamentals - basic | WeiYuan
Intro to Python
Ggplot2 v3
python language programming presentation
Replication and Replica Sets
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)

More from rfyiamcool (12)

PDF
Redis cluster那些事儿
PDF
Golang advance
PDF
Golang 高性能实战
PDF
Mysql fast share
PPTX
分析mysql acid 设计实现
PPTX
Raft
PPTX
大话redis设计实现
PPTX
python gil
PPTX
async io frame
PDF
异步io框架的实现
PPTX
美妙的多进程管理
PDF
聊聊我接触的集群管理
Redis cluster那些事儿
Golang advance
Golang 高性能实战
Mysql fast share
分析mysql acid 设计实现
Raft
大话redis设计实现
python gil
async io frame
异步io框架的实现
美妙的多进程管理
聊聊我接触的集群管理

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
medical staffing services at VALiNTRY
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
history of c programming in notes for students .pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
Reimagine Home Health with the Power of Agentic AI​
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Advanced SystemCare Ultimate Crack + Portable (2025)
Monitoring Stack: Grafana, Loki & Promtail
medical staffing services at VALiNTRY
Patient Appointment Booking in Odoo with online payment
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
history of c programming in notes for students .pptx
L1 - Introduction to python Backend.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Computer Software and OS of computer science of grade 11.pptx
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Designing Intelligence for the Shop Floor.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Complete Guide to Website Development in Malaysia for SMEs
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Why Generative AI is the Future of Content, Code & Creativity?

python高级内存管理

  • 2. Object-specific allocators _____ ______ ______ ________ [ 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) --> |
  • 3. * Request in bytes Size of allocated block Size class idx * ---------------------------------------------------------------- * 1-8 8 0 * 9-16 16 1 * 17-24 24 2 * 25-32 32 3 * 33-40 40 4 * 41-48 48 5 * 49-56 56 6 * 57-64 64 7 * ... ... ... * 497-504 504 62 * 505-512 512 63 * * */
  • 5. method posix malloc python memory pool object buffer pool
  • 6. Arena FeeePool Pool Pool Pool Pool Headers No BLock Arena malloc heap & pool Process stack heap bss init data text UserPool 1-8 … … 249 - 256 Pool Free Block Free Block Use Block
  • 7. userdpool design UserdPools 1-8 9-16 17-24 … … 249-256 Pool Header Free Block Free Block Header 分配 回收 … Pool Free Block Free Block Use Block 同⼀个Pool下Block⼀样长 单Pool为4kb Block及Pool都为单链表
  • 8. free pool desgin FeeePool Pool Pool Pool … Pool Headers No BLock Pool Headers No BLock Pool为4kb⼤小 Pool清理Headers
  • 9. where store variable ? run-time Stack list dict int heap [1 ,2, 3] {“n”: “1”} 1
  • 10. why ? In [1]: a = 123 In [2]: b = 123 In [3]: a is b Out[3]: True In [4]: a = 1000 In [5]: b = 1000 In [6]: a is b Out[6]: False In [7]: a = 'n' In [8]: b = 'n' In [9]: a is b Out[9]: True In [10]: a = "python" In [11]: b = "python" In [12]: a is b Out[12]: True
  • 11. why ? In [1]: def go(var): ...: print id(var) …: In [2]: id(a) Out[2]: 4401335072 In [3]: go(a) 4401335072 In [10]: a = b = 'nima' In [11]: b = a In [12]: a is b Out[12]: True In [13]: b = 'hehe' In [14]: a is b Out[14]: False 只有引用 ?
  • 12. python objects stored in memory? names names object Python Has Names, Not Variables ! ! !
  • 13. 整数对象池 -5 -4 … 0 … 256 257 小整数 ⼤整数 … … -5 -4 … … 257 … … var_1 var_2 … … -5 -4 … … 257 … … var_3 var_4 the same addr ! not the same addr ! 28 bytes 解释器初始化
  • 14. 整数对象池 Block List Free List PyIntBlock PyIntBlock PyIntBlock PyIntBlock 不会归还给 Arena和os ! ! !
  • 15. 字符对象池 a b c d … … … var_1 var_2 the same addr ! 单个字符38 bytes 由解释器初始化
  • 16. 字符串对象池 aa en cao oh woyao buyao kuai feile var_1 0 1 2 3 … var_2 hash存储变量 共用地址 记录引用计数 ref
  • 17. ref count x = 300 y = x z = [x, y] X ref += 1 300 y Z ref += 1 ref += 2 References -> 4 !
  • 18. What does del do? x = 300 y = x del x X ref -= 1 300 y References -> 1!The del statement doesn’t delete objects. • removes that name as a reference to that object • reduces the ref count by 1
  • 19. ref count case def go(): w = 300 go() a = “fuc . ” del a b = “en, a” b = None ref count +1 w is out of scope; ref count -1 del a; ref count -1 重新赋值; ref count -1
  • 20. cyclical refclass Node: def __init__(self, va): self.va = va def next(self, next): self.next = next mid = Node(‘root’) left = Node(‘left’) right = Node(‘right’) mid(left) left.next(right) right.next(left) Mid rightleft if del mid node: how ?
  • 21. mark & sweep gc root b a w c K G R
  • 22. 分代回收 node node node node node node node node node node node node node node node node node node node
  • 23. 可变 vs 不可变 (obj) string int tuple list dict
  • 24. container objects a = [10, 10, 11] b = a PyListObject Type list rc 1 items size … … PyObject Type integer rc 2 value 10 PyObject Type integer rc 1 value 11 10 10 11
  • 25. copy.copy a = [10, 10, [10, 11] ] b = copy.copy(a) PyListObject Type list rc 1 items size … … PyObject Type integer rc 2 value 10 PyObject Type integer rc 1 value 11 10 10 ref PyListObject Type list rc 1 items size … … 10 10 ref PyListObject 10 11
  • 26. copy.deepcopy a = [10, [ 10, 11 ] ] b = copy.deep(a) PyListObject Type list rc 1 items size … … PyObject Type integer rc 2 value 10 PyObject Type integer rc 1 value 11 10 ref PyListObject Type list rc 1 items size … … 10 ref PyListObject 10 11 PyListObject 10 11
  • 27. diy gc import gc import sys gc.set_debug(gc.DEBUG_STATS|gc.DEBUG_LEAK) a=[] b=[] a.append(b) print 'a refcount:',sys.getrefcount(a) # 2 print 'b refcount:',sys.getrefcount(b) # 3 del a del b print gc.collect() # 0
  • 28. Garbage Collector Optimize memory bound 可以降低threshold来时间换空间 cpu bound 提⾼threshold来空间换时间 暂停gc, 引⼊master worker设计
  • 29. 引用计数 跟 gil 的影响 ? gc 是否是原⼦ ? gc的 stop the world现象 ? … Q & A
  • 30. “ END ” – xiaorui.cc