Memory clean script1/6/2024 ![]() When CPython handles memory, it uses the GIL to ensure that it does so safely. 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. In other words, only one author can write at a time. 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). The end result is two stories on top of each other, which makes the whole page completely unreadable. They each ignore the other’s attempt to craft a story and begin writing on the page. Not only that, but they both need to write on the same page of the book at the same time. Suppose that two authors stubbornly decide that it’s their turn to write. The end result can be a garbled mess where neither of the threads ends up with what they wanted.Ĭonsider the book analogy again. When two threads try to modify the same resource at the same time, they can step on each other’s toes. The GIL is a solution to the common problem of dealing with shared resources, like memory in a computer. Remove ads The Global Interpreter Lock (GIL) It converts your Python code into instructions that it then runs on a virtual machine. The default Python implementation fulfills both of those requirements. You also need something to actually execute interpreted code on a computer. You still need something to interpret written code based on the rules in the manual. ![]() However, that manual isn’t all that useful by itself. The Python language is defined in a reference manual written in English. A language that’s written in another language?! Well, not really, but sort of. When I first heard this, it blew my mind. The default Python implementation, CPython, is actually written in the C programming language. The algorithms and structures that the Python application uses for memory management is the focus of this article. It carries out (or denies) requests to read and write memory.Ībove the OS, there are applications, one of which is the default Python implementation (included in your OS or downloaded from .) Memory management for your Python code is handled by the Python application. One of the main layers above the hardware (such as RAM or a hard drive) is the operating system (OS). There are many layers of abstraction that the Python code goes through before the objects actually get to the hardware though. Somewhere in your computer, there’s a physical device storing data when you’re running your Python programs. But freed to where? Where did this “memory” come from? On the flip side, when data is no longer needed, it can be deleted, or freed. This process of providing memory is generally called memory allocation. 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. ![]() A memory manager determines where to put an application’s data. Memory management is the process by which applications read and write data. Remove ads Memory Management: From Hardware to Software The person who removed the old stories to make room for new ones is a garbage collector. The manager, who decides where the authors can write in the book, plays the role of a memory manager of sorts. The authors are like different applications or processes that need to store data in memory. In fact, it’s common to call fixed-length contiguous blocks of memory pages, so this analogy holds pretty well. In essence, computer memory is like that empty book. When no one reads or references the stories, they are removed to make room for new stories. Since this book is around for a long time, many of the stories in it are no longer relevant. The manager then decides where in the book they’re allowed to write. Before they begin writing, they consult the manager of the book. Since they aren’t allowed to write over each other, they must be careful about which pages they write in. Each author wants some space to write their story in. Eventually, different authors will come along. There’s nothing written on the pages yet. You can begin by thinking of a computer’s memory as an empty book intended for short stories. Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |