-
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 writen 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.
In essence, computer memory is like that empty book, maybe this why it’s common to call fiexd-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, palys 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 anology, 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.
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.
-
The Default Python Implementation
The default Python implementation, CPython , is acutally written in the C programming language.
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 mannual 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.
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.
Virtual Machines are like physical computers, but they are implemented in software. They typically process basic instructions similar to ***
理解Python中的内存管理memory management
最新推荐文章于 2025-08-01 10:04:40 发布

最低0.47元/天 解锁文章
854





