Python garbage collection mechanism

Memory management is a complex field of computer science, and many techniques are currently being developed to improve its efficiency.

Insert picture description here

Memory management is usually divided into three areas##

Memory management is usually divided into three areas, although the distinction is a bit vague:

The basic problem of managing memory is knowing when to keep the data it contains and when to discard it so that the memory can be reused. This sounds easy, but it is actually a difficult problem, and it is the entire field of research in itself. In an ideal world, most programmers would not have to worry about memory management issues. Unfortunately, in manual and automatic memory management, poor memory management practices can affect the robustness and speed of a program in many ways.

Automatic memory management is a service that can be used as part of the language or as an extension to automatically reclaim memory that is no longer used by the program. Automatic memory managers (usually called garbage collectors, or collectors for short) usually do their work by reclaiming blocks that are inaccessible to program variables (that is, blocks that are inaccessible to subsequent pointers).

Automatic memory management##

The advantages of automatic memory management are:

The disadvantages of automatic memory management are:

There are many ways to perform automatic memory reclamation, several of which are discussed in reclamation techniques.
Most modern languages mainly use automatic memory management: BASIC, Dylan, Erlang, Haskell, Java, JavaScript, Lisp, ML, Modula-3, Perl, PostScript, Prolog, Python, Scheme, Smalltalk, etc.

Insert picture description here

Python

Python runs several implementations on multiple virtual machines: the original "CPython" implementation runs on its own virtual machine; IronPython runs on the common language runtime; Jython on the Java virtual machine.

CPython manages memory by mixing reference counting and non-moving mark cleanup garbage collection. The reference count ensures that the object is quickly deleted when its reference count drops to zero, while the garbage collector recycles the loop data structure.

__ del__ and weak references (via weakref module).

Garbage collection in Python is based on reference counting, supplemented by generational collection. The defect of reference counting is the problem of circular references.

In Python, if the reference count of an object is 0, the Python virtual machine will reclaim the memory of this object.

  1. Memory Management Reference
  2. FOOFISH-PYTHON Zen—Garbage Collection Mechanism in Python
  3. gc—garbage collector interface
  4. Detailed explanation of Python garbage collection mechanism
  5. Python garbage collection mechanism

Recommended Posts

Python garbage collection mechanism
Detailed analysis of Python garbage collection mechanism
Learn about garbage collection in Python
Python interview questions collection (three)
Python common exception handling mechanism
Collection of Python Common Modules
Detailed implementation of Python plug-in mechanism
01. Introduction to Python
Introduction to Python
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python garbage collection mechanism