Member-only story
How Does Garbage Collection Work in Python
Several decades ago, computer memory was extremely scarce. Initially, the entire RAM of a computer was measured in kilobytes, and when the Intel 286 processor came out in 1982, it could address a whopping 16 megabytes of RAM. In those days, programmers had to worry about memory a lot, so the programming languages at the time all mandated manual memory management. This meant that they had to manually allocate a certain amount of memory before they could declare a variable, and after they were done with that variable, they needed to free this memory in order to avoid memory leaks. Most bugs occurred if memory was freed to soon, or if it was not freed in time (or at all). A good example of how efficient programmers had to be with memory in those days is the fact that Bill Gates wrote the entire Microsoft Basic interpreter so that it occupied only 4 kilobytes of memory.
Luckily, technology advanced during the years, and RAM became abundant, leading the way for a new generation of programming languages- garbage collected ones.
In this article I will talk about the following topics:
- What is garbage collection
- How is it implemented in Python
- The gc module, and how you can manipulate the Python garbage collector