-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_management_in_python.txt
487 lines (400 loc) · 28.8 KB
/
memory_management_in_python.txt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
https://realpython.com/python-memory-management/
Memory Management in Python
Python内存管理
Ever wonder how Python handles your data behind the scenes? How are your variables stored in memory? When do they get deleted?
是否曾想过Pythno怎样在幕后管理数据?变量是怎样存储在内存中的?什么时候会被删除?
In this article, we’re going to do a deep dive into the internals of Python to understand how it handles memory management.
在这篇文章中,我们将深入python内部来探究内存管理。
By the end of this article, you’ll:
* Learn more about low-level computing, specifically as relates to memory
* Understand how Python abstracts lower-level operations
* Learn about Python’s internal memory management algorithms
读完这篇文章,你将:
* 了解更多关于底层计算逻辑,尤其是内存相关方面。
* 理解Python怎样对底层操作进行抽象
* 明白Python内存管理的的算法
Understanding Python’s internals will also give you better insight into some of Python’s behaviors.
Hopefully, you’ll gain a new appreciation for Python as well.
So much logic is happening behind the scenes to ensure your program works the way you expect.
探究Python内部原理能让你有个更好的视角观察python。
希望你也会对Python有个新的认识。
在你的的程序能正常运行的背后也有巨量的python逻辑上演着。
Memory Is an Empty Book
You can begin by thinking of a computer’s memory as an empty book intended for short stories.
There’s nothing written on the pages yet. Eventually, different authors will come along. Each author wants some space to write their story in.
内存是一本空白的书
首先,你可以把计算机的内存想象成一本写短篇故事的空白书。
当前的每一页都是空的。
最终,不同的作者会参与进来。
每个作者都会得到一些页面来写入他们的故事。
Since they aren’t allowed to write over each other, they must be careful about which pages they write in.
Before they begin writing, they consult the manager of the book.
The manager then decides where in the book they’re allowed to write.
他们得写的很小心因为不能把东西写到其他人的页面上,
在他们写之前,他们会和经理商量一下。
经理来决定他们允许写到哪些页上。
Since this book is around for a long time, many of the stories in it are no longer relevant.
When no one reads or references the stories, they are removed to make room for new stories.
因为这书也写了很久了,很多故事已经没什么意义了。
当一个故事没人看或没人提及时,就会被删掉,留下页给新的故事。
In essence, computer memory is like that empty book.
In fact, it’s common to call fixed-length contiguous blocks of memory pages, so this analogy holds pretty well.
本质上,计算机内存就像那本空白的书。
实际上,通常将固定长度的连续内存称为内存页,因此这个比喻很相似。
The authors are like different applications or processes that need to store data in memory.
The manager, who decides where the authors can write in the book, plays the role of a memory manager of sorts.
The person who removed the old stories to make room for new ones is a garbage collector.
书的作者就像需要存数据到内存的应用或进程。
经理决定作者可以在书中何处写入内容,他扮演着类似内存管理器的角色。
删掉旧的故事给新故事腾出空白页的人就是垃圾回收器。
Memory Management: From Hardware to Software
Memory management is the process by which applications read and write data.
A memory manager determines where to put an application’s data. Since there’s a finite chunk of memory,
like the pages in our book analogy, the manager has to find some free space and provide it to the application.
This process of providing memory is generally called memory allocation.
内存管理:从硬件到软件
内存管理是指应用程序读写数据的过程。
内存管理器决定把应用数据放在哪。
因为内存是有限的,就跟前面书的比喻一样,内存管理器得找一些空位给程序。
提供内存空间的过程通常叫内存分配。
On the flip side, when data is no longer needed, it can be deleted, or freed.
But freed to where? Where did this “memory” come from?
另一方面,当数据不再需要时,可以被删除或释放。
但释放到哪里呢?
这些内存又是从哪里来的?
Somewhere in your computer, there’s a physical device storing data when you’re running your Python programs.
There are many layers of abstraction that the Python code goes through before the objects actually get to the hardware though.
在计算机内部,有个物理设备存储着正在运行的Python程序的数据。
在python代码和硬件之间隔着很多抽象层。
One of the main layers above the hardware (such as RAM or a hard drive) is the operating system (OS).
It carries out (or denies) requests to read and write memory.
其中在硬件(比如内存,硬盘)上面的最主要一层是操作系统。
Above the OS, there are applications, one of which is the default Python implementation (included in your OS or downloaded from python.org.)
Memory management for your Python code is handled by the Python application.
The algorithms and structures that the Python application uses for memory management is the focus of this article.
操作系统之上就是程序了,其中就有Python的默认实现版(内置在操作系统或从python.org下载的)。
Python代码的内存管理由Python程序负责。
Python程序用于内存管理的算法和数据结构就是本篇文章的主旨。
The Default Python Implementation
The default Python implementation, CPython, is actually written in the C programming language.
Python的默认实现版
Python的默认实现版叫CPython,是C写的。
When I first heard this, it blew my mind.
A language that’s written in another language?! Well, not really, but sort of.
第一次知道的时候让我很惊讶。
一门语言由另一门语言编写?!好吧,并不全是,但也差不多。
The Python language is defined in a reference manual written in English.
However, that manual isn’t all that useful by itself.
You still need something to interpret written code based on the rules in the manual.
Python这门语言的定义是由英语写在参考手册上的。
然后手册本身并没有什么很大作用。
你仍需要参考手册中的规则写出一些解析代码。
Note: Virtual machines are like physical computers, but they are implemented in software.
They typically process basic instructions similar to Assembly instructions.
注意:虚拟机就像硬件机,但是由软件实现的。
典型的基于指令的处理过程和汇编指令很相似。
Python is an interpreted programming language.
Your Python code actually gets compiled down to more computer-readable instructions called bytecode.
These instructions get interpreted by a virtual machine when you run your code.
Python是解释执行的语言。
你的Python代码实际上会被编译成计算机更能识别的叫字节码的指令。
当你运行代码的时候这些指令由虚拟机解析出来。
You also need something to actually execute interpreted code on a computer.
The default Python implementation fulfills both of those requirements.
It converts your Python code into instructions that it then runs on a virtual machine.
同时你也需要能在计算机中实际执行这些字节码的东西。
默认的Python实现包含了这以上两样。
It’s important to note that there are implementations other than CPython.
IronPython compiles down to run on Microsoft’s Common Language Runtime.
Jython compiles down to Java bytecode to run on the Java Virtual Machine.
Then there’s PyPy, but that deserves its own entire article, so I’ll just mention it in passing.
需要了解的是除了CPython外还有很多其他实现。
IronPython被编译成在微软的公共语言运行时上运行。
Jython将编译为Java字节码在Java虚拟机上运行。
还有PyPy,关于它还得另起一篇文章,还是一笔带过先。
For the purposes of this article,
I’ll focus on the memory management done by the default implementation of Python, CPython.
这篇文章主要集中在Python默认实现CPython是如何管理内存上。
Disclaimer: While a lot of this information will carry through to new versions of Python, things may change in the future.
Note that the referenced version for this article is the current latest version of Python, 3.7.
声明:Python每个版本的发布都会有很多改变。当前篇幅主要讨论的是Python3.7版。
Okay, so CPython is written in C, and it interprets Python bytecode. What does this have to do with memory management?
Well, the memory management algorithms and structures exist in the CPython code, in C.
To understand the memory management of Python, you have to get a basic understanding of CPython itself.
说回来,CPython是用C写的并可以解析Python字节码。这些和内存管理又有什么关联呢?
因为内存管理的算法和数据结构就在C写的CPython代码里。
要理解Python的内存管理机制,就得对CPython本身有个基本的了解。
You may have heard that everything in Python is an object, even types such as int and str.
Well, it’s true on an implementation level in CPython. There is a struct called a PyObject, which every other object in CPython uses.
也许你听说过在Python里面一切皆对象,包括像int或str这样的类型本身。
Note: A struct, or structure, in C is a custom data type that groups together different data types.
To compare to object-oriented languages, it’s like a class with attributes and no methods.
注意:在C语言里面一个struct就是一组不同类型数据的集合。
可以类比为面向对象语言里面一个只有属性没有方法的类。
CPython is written in C, which does not natively support object-oriented programming.
Because of that, there are quite a bit of interesting designs in the CPython code.
CPython是用没有原生面向对象支持的C语言写的。
所以,在CPython的代码里面有很多有趣的设计。
The PyObject, the grand-daddy of all objects in Python, contains only two things:
PyObject在Python里面是所有对象的鼻祖,它只包含两样东西:
ob_refcnt: reference count
ob_type: pointer to another type
ob_refcnt: 引用计数
ob_type: 类型指针
The reference count is used for garbage collection.
Then you have a pointer to the actual object type.
That object type is just another struct that describes a Python object (such as a dict or int).
引用计数是用于垃圾回收的。
类型指针则是指向另一实体类型的指针。
那个类型的指针只是另一个描述Python实体的struct(比如dict或int)。
Each object has its own object-specific memory allocator that knows how to get the memory to store that object.
Each object also has an object-specific memory deallocator that “frees” the memory once it’s no longer needed.
每个实体包含特定的内存分配器,用于申请内存和存储自身。
每个实体也有特定的内存释放器用于当自身不被引用时的内存释放。
However, there’s an important factor in all this talk about allocating and freeing memory.
Memory is a shared resource on the computer, and bad things can happen if two different processes try to write to the same location at the same time.
与此同时,在申请和释放内存时还有个很重要的因素。
内存在计算机内是共享的资源,如果两个不同的进程同时使用一块相同的内存则会出错。
The Global Interpreter Lock (GIL)
The GIL is a solution to the common problem of dealing with shared resources, like memory in a computer.
When two threads try to modify the same resource at the same time, they can step on each other’s toes.
The end result can be a garbled mess where neither of the threads ends up with what they wanted.
全局解释器锁(GIL)
GIL是解决计算机中如内存之类的共享资源的通用解决方案。
当两个线程试图同时修改相同的资源时,它们可能会影响到对方。
最终结果可能是都得不到自己想要的结果。
Consider the book analogy again.
Suppose that two authors stubbornly decide that it’s their turn to write.
Not only that, but they both need to write on the same page of the book at the same time.
再拿书本来打个比方。
想象一下两个作者固执的坚持本次该轮到自己来书写。
不仅如此,他们写的还是同一页纸。
They each ignore the other’s attempt to craft a story and begin writing on the page.
The end result is two stories on top of each other, which makes the whole page completely unreadable.
他们都忽略掉对方然后自己开始在这一页上写故事。
结果是两个故事互相交织在一起,整页都没人看得懂。
One solution to this problem is a single, global lock on the interpreter when a thread is interacting with the shared resource (the page in the book).
In other words, only one author can write at a time.
有个解决方案是当线程影响到共享资源(书本中的空白页)的时候有唯一一个全局的的锁来锁住解释器。
换句话说,同事只有一个作者可写。
Python’s GIL accomplishes this by locking the entire interpreter, meaning that it’s not possible for another thread to step on the current one.
When CPython handles memory, it uses the GIL to ensure that it does so safely.
Python的GIL通过加锁整个解释器来获得资格,就是说另一个线程不会影响到当前这个。
当CPython对内存进行处理的时候,使用GIL来确保这些操作是安全的。
There are pros and cons to this approach, and the GIL is heavily debated in the Python community.
To read more about the GIL, I suggest checking out What is the Python Global Interpreter Lock (GIL)?
这种方式有它的优点和缺点,Python社区对GIL的争论很激烈。
想要了解更多GIL的知识,我建议你们可以看看《什么是全局解释器锁》(https://realpython.com/python-gil/?from=ethan)这篇文章。
Garbage Collection
垃圾回收
Let’s revisit the book analogy and assume that some of the stories in the book are getting very old.
No one is reading or referencing those stories anymore.
If no one is reading something or referencing it in their own work, you could get rid of it to make room for new writing.
我们再看一下书的类比,假设其中一些故事已经过时了。
没人看也没人引用这些故事。
这种情况下就该处理掉这些故事以便腾出新的页面。
That old, unreferenced writing could be compared to an object in Python whose reference count has dropped to 0.
Remember that every object in Python has a reference count and a pointer to a type.
这些没人看和引用的故事就像Python里面引用计数为0的对象。
提醒一下每个实体对象在Python中都有一个引用计数和类型指针。
The reference count gets increased for a few different reasons.
For example, the reference count will increase if you assign it to another variable:
有几个不同的因素可让引用计数增长。
比如,当前对象被赋予其他变量时引用计数会增长。
numbers = [1, 2, 3]
# 引用计数 = 1
more_numbers = numbers
# 引用计数 = 2
It will also increase if you pass the object as an argument:
当把对象传参使用的时候也会增加引用计数:
total = sum(numbers)
As a final example, the reference count will increase if you include the object in a list:
最后举个例子,当一个list包含此对象的时候也会增加引用计数:
matrix = [numbers, numbers, numbers]
Python allows you to inspect the current reference count of an object with the sys module.
You can use sys.getrefcount(numbers), but keep in mind that passing in the object to getrefcount() increases the reference count by 1.
你可以通过sys模块来检查Python对象的引用计数。
你可以这样用sys.getrefcount(numbers), 但要记得当你用getrefcount()的时候numbers的引用计数也会加1。
In any case, if the object is still required to hang around in your code, its reference count is greater than 0.
Once it drops to 0, the object has a specific deallocation function that is called which “frees” the memory so that other objects can use it.
任何情况下,如果一个对象仍在你代码某处被使用,那它的引用计数就会大于0.
一旦降为0的时候,这个对象特定的释放函数就会被调用来释放内存给其他对象复用。
But what does it mean to “free” the memory, and how do other objects use it?
Let’s jump right into CPython’s memory management.
所谓的“释放”到底是什么意思呢?其他对象又是如何复用这块内存的?
让我们深入CPython的内存管理机制。
CPython’s Memory Management
We’re going to dive deep into CPython’s memory architecture and algorithms, so buckle up.
CPython的内存管理
准备好,我们即将深入研究CPython的内存结构和算法。
As mentioned before, there are layers of abstraction from the physical hardware to CPython.
The operating system (OS) abstracts the physical memory and creates a virtual memory layer that applications (including Python) can access.
如上所述,在硬件和CPython之间还有很多抽象层。
操作系统对实体内存做了抽象并建立一个虚拟内存层给程序(包括Python)来访问。
Python uses a portion of the memory for internal use and non-object memory.
The other portion is dedicated to object storage (your int, dict, and the like).
Note that this was somewhat simplified.
If you want the full picture, you can check out the CPython source code, where all this memory management happens.
Python留了一块内存来给对象之外的内部使用。
其他部分取决于对象如何存储(你的int,dict等等)
如果你想要个全面的了解,可以看下CPython的源码,所有内存管理相关的都在里面。
CPython has an object allocator that is responsible for allocating memory within the object memory area.
This object allocator is where most of the magic happens.
It gets called every time a new object needs space allocated or deleted.
CPython有一个内存分配器来负责在对象内存区分配内存。
这个对象分配器就是所有魔法发生的源头。
每当一个新的对象需要分配或释放时都会调用。
Typically, the adding and removing of data for Python objects like list and int doesn’t involve too much data at a time.
So the design of the allocator is tuned to work well with small amounts of data at a time.
It also tries not to allocate memory until it’s absolutely required.
像典型的int或list等Python对象在每次分配和释放时不会包含太多的数据。
所以分配器被设计成在分配少批量数据时如何更好的工作。
同时也要避免不要当真的需要内存的时候才去申请物理内存。
The comments in the source code describe the allocator as
“a fast, special-purpose memory allocator for small blocks, to be used on top of a general-purpose malloc.”
In this case, malloc is C’s library function for memory allocation.
源码里面关于分配器的描述是:
一种快速且为小块内存专用的分配器,用于通用malloc之上。
此处讲的malloc是C里面用于分配内存的库函数。
Now we’ll look at CPython’s memory allocation strategy.
First, we’ll talk about the 3 main pieces and how they relate to each other.
现在我们来看看CPython的内存分配策略。
首先,我们先讲一下3个互相影响的区。
Arenas are the largest chunks of memory and are aligned on a page boundary in memory.
A page boundary is the edge of a fixed-length contiguous chunk of memory that the OS uses.
Python assumes the system’s page size is 256 kilobytes.
arenas区是内存中最大的块,在内存中是按页对齐的。
页是指被操作系统使用的一小块连续且固定大小的内存块。
Python假设操作系统使用的页大小是256K。
Within the arenas are pools, which are one virtual memory page (4 kilobytes).
These are like the pages in our book analogy.
These pools are fragmented into smaller blocks of memory.
arenas区就是几个使用虚拟内存页(4K)的内存池。
就像我们类比书里面的空白页面。
这些内存池被切分成更小的内存块。
All the blocks in a given pool are of the same “size class.”
A size class defines a specific block size, given some amount of requested data.
The chart below is taken directly from the source code comments:
同个内存池内的所有块大小均相同。
给定一组请求数据,规格类定义了指定块。
以下图表是从源码注释转换而来:
[]
For example, if 42 bytes are requested, the data would be placed into a size 48-byte block.
例如,如果需要42个字节,那么数据会存放在一个48字节的块中。
Pools
Pools are composed of blocks from a single size class.
Each pool maintains a double-linked list to other pools of the same size class.
In that way, the algorithm can easily find available space for a given block size, even across different pools.
内存池
内存池是由相同规格类定义的块组成。
每个内存池都管理着一个双向链表,链接着其他相同规格的内存池。
由此算法可以很容易的通过给定的块大小找到可用空间,甚至是在不同内存池之间也行。
A usedpools list tracks all the pools that have some space available for data for each size class.
When a given block size is requested, the algorithm checks this usedpools list for the list of pools for that block size.
可通过已使用的内存池列表追踪所有相同规格类的可用空间。
给定一个块大小,算法可以从已使用内存池列表中检测出来。
Pools themselves must be in one of 3 states: used, full, or empty.
A used pool has available blocks for data to be stored.
A full pool’s blocks are all allocated and contain data.
An empty pool has no data stored and can be assigned any size class for blocks when needed.
内存池必须是以下3种状态之一:使用中,满,空。
使用中的内存池有特定大小块可供数据存储。
满的内存池内被已分配的数据占满。
空内存池没有数据,当需要的时候可以被初始化为任意大小规格的内存池。
A freepools list keeps track of all the pools in the empty state.
But when do empty pools get used?
空内存池列表记录着所有空状态的内存池。
那空内存池什么时候会被用到呢?
Assume your code needs an 8-byte chunk of memory.
If there are no pools in usedpools of the 8-byte size class, a fresh empty pool is initialized to store 8-byte blocks.
This new pool then gets added to the usedpools list so it can be used for future requests.
假设你的代码需要8个字节的内存池块。
如果在已使用的内存池列表中没有关于8个字节规格的,那么一个空的内存池会被初始化为专门存储8个字节的块。
同时这个新的内存池会被添加到已使用内存池中供接下来的请求使用。
Say a full pool frees some of its blocks because the memory is no longer needed.
That pool would get added back to the usedpools list for its size class.
当满的内存池当中有些块被回收了,
那么这个内存池又会被添加到当前大小的使用中内存池列表中。
You can see now how pools can move between these states (and even memory size classes) freely with this algorithm.
现在你知道这些内存池是怎样从不同状态之间自由切换的算法了。
[]
As seen in the diagram above, pools contain a pointer to their “free” blocks of memory.
There’s a slight nuance to the way this works.
This allocator “strives at all levels (arena, pool, and block) never to touch a piece of memory until it’s actually needed,”
according to the comments in the source code.
由上图可知,内存池包含一个指向空内存块的指针。
这里有一点细微的差别。
源代码的注释指出,分配器力求在各级别(arena, pool, block)内存真正被需要的时候才去使用它。
That means that a pool can have blocks in 3 states. These states can be defined as follows:
内存池中的块有3种状态。
这些状态的定义如下:
untouched: a portion of memory that has not been allocated
free: a portion of memory that was allocated but later made “free” by CPython and that no longer contains relevant data
allocated: a portion of memory that actually contains relevant data
untouched: 还未被分配使用的内存块
free: 被分配然后又被"释放"的内存块且里面没有保存相关数据了
allocated: 已分配且含有数据的内存块
The freeblock pointer points to a singly linked list of free blocks of memory.
In other words, a list of available places to put data.
If more than the available free blocks are needed, the allocator will get some untouched blocks in the pool.
free状态的块指针列表保存着一系列的free态内存。
换句话说,一个可用来放数据的列表。
如果需要比可用的所有free态内存还要多,那么分配器会去使用那些untouched态的块。
As the memory manager makes blocks “free,” those now free blocks get added to the front of the freeblock list.
The actual list may not be contiguous blocks of memory, like the first nice diagram.
It may look something like the diagram below:
当内存管理器把内存块状态置为"释放"时会把它添加到free态链表的头部。
这个链表可能不像上面那图一样为连续的内存块。
它可能是如下图那样:
[]
Arenas
Arenas contain pools. Those pools can be used, full, or empty.
Arenas themselves don’t have as explicit states as pools do though.
Arenas区包含着内存池。这些内存池可以是使用中,满,或空的。
Arenas区不像内存池那样有明显的状态区分。
Arenas are instead organized into a doubly linked list called usable_arenas.
The list is sorted by the number of free pools available.
The fewer free pools, the closer the arena is to the front of the list.
Arenas区由称为usable_arenas的双向链表组织而成。
此链表按可用内存池的数量排序。
越少可用内存池的排在越前面。
[]
This means that the arena that is the most full of data will be selected to place new data into.
But why not the opposite? Why not place data where there’s the most available space?
这意味着arena区会选择更接近用满的地方来存放数据。
为什么反过来做呢?
为什么数据不放到最空的地方去?
This brings us to the idea of truly freeing memory.
You’ll notice that I’ve been saying “free” in quotes quite a bit.
The reason is that when a block is deemed “free”, that memory is not actually freed back to the operating system.
The Python process keeps it allocated and will use it later for new data.
Truly freeing memory returns it to the operating system to use.
这就要说到真正的内存释放。
你也许注意到我给释放加了引号, 它并不是真正的释放到操作系统。
Python继续保留着以供新的数据使用。
真正的内存释放是返回给操作系统使用。
Arenas are the only things that can truly be freed.
So, it stands to reason that those arenas that are closer to being empty should be allowed to become empty.
That way, that chunk of memory can be truly freed, reducing the overall memory footprint of your Python program.
arenas区是唯一可以真正被释放的地方。
所以那些接近为空的区域也理所当然可以为空。
通过这种方式,可以真正释放内存,减少Python程序的总体内存占用。
[]
Conclusion
Memory management is an integral part of working with computers.
Python handles nearly all of it behind the scenes, for better or for worse.
总结
内存管理是计算机工作中不可或缺的一部分。
不管好坏,Python几乎在幕后处理所有这些问题。
In this article, you learned:
在本篇中,你学到了:
What memory management is and why it’s important
How the default Python implementation, CPython, is written in the C programming language
How the data structures and algorithms work together in CPython’s memory management to handle your data
Python abstracts away a lot of the gritty details of working with computers.
This gives you the power to work on a higher level to develop your code without the headache of worrying about how and where all those bytes are getting stored.
什么是内存管理和为什么它很重要
默认的Python实现CPython是用C写的。
CPython的内存管理是怎样通过数据结构和算法来管理你的数据的。
Python抽象了很多繁杂的细节来与计算机打交道。
这使得你有能力从更高的层次来开发你的代码而不用为这些字节存放到哪而头疼。