• Garbage Collection in Python
  • Originally written by Raivat Shah
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: chaingangway
  • Proofreader: Jiang Don’t know, PingHGao

Python memory management garbage collection

If you’ve been coding for a while, you’ve probably heard of garbage collection. In this article, we will delve more deeply into its functions and principles.

What and according to

In the real world, we clear things out — old notes, boxes we no longer need — and throw them in the trash or recycling bin. Since storage space is limited, we need to make room for other important items.

Similarly, in computers, space — also known as memory — is an important and limited resource. Therefore, the garbage collector collects data objects that are no longer needed and throws them away.

Garbage collection can be automatic or manual, depending on the programming language. In most high-level languages, such as Python and Java, it is automatic. Therefore, these languages are called garbage collection languages. Other languages (such as C) do not support automatic garbage collection; the programmer is responsible for memory management.

Let’s look at how garbage collection works.

How

There are different techniques for garbage collection, but most garbage collection languages (including Python) use reference counting. In reference counting, we record the number of references to an object and discard the object when the count reaches zero.

The reference count of an object changes as the number of aliases pointing to the object changes. The reference count increases when an object is assigned a new name or placed in a container such as a list or dictionary. The count is reduced when objects are deleted, references are out of scope, or objects are reassigned using the del command. Such as:

sample = 100 # create <100> object, reference count = 1.

sample_copy = sample Reference count = 2.

sample_list = [sample] Reference count = 3.

del sample Reference count = 2. Note that sample_copy and sample_list are not affected because they point directly to <100>.
sample_copy = 10 # reference count = 1 because the variable is reassigned.

sample_list.clear() # reference count = 0, no variables to <100> will be stored when the list is cleared.
Copy the code

When the reference count drops to zero, the object is immediately reclaimed. But there is a cost to doing this: we need to store an additional integer value for each object to indicate its reference count (a tradeoff between space and time).

However, the problem of circular reference exists in reference counting system. If two objects A and B refer to each other, their reference count will always be greater than or equal to 1. This is common with lists, classes, and functions. For example, when an object references itself:

x = []
x.append(x)
Copy the code

Or when objects refer to each other:

a.attribute_1 = b
b.attribute_2 = a
Copy the code

The garbage collector periodically finds circular references and removes them. This process is expensive, so it is performed regularly and can be planned. The garbage collector interface in Python provides methods that you can use to explore the schedule and thresholds for performing garbage collection.

Gc – Garbage Collector Interface – Python 3.8.3 rC1 documentation


conclusion

Hope this article can help you. Please read further on the following resources:

  • How does garbage collection work in Python?
  • What is a circular reference in Python?

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.