理解Python中的内存管理memory management

  • 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 ***

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值