Advanced languages such as Java, C #, etc., use garbage collection instead of the user-managed and maintained memory of C and C ++. Self-management memory is extremely free, you can apply for memory arbitrarily, but like a double-edged sword, for a large number of memory leaks, dangling Pointers and other bugs buried hidden dangers. A string, a list, a class, and even a value are all objects in a language that is easy to locate and use, and naturally does not leave the user to deal with how to allocate reclaimed memory. Python also uses the same garbage collection mechanism as Java, but the difference is that Python uses reference counting as the main mechanism, and mark-sweep and generational collection as the secondary mechanism

Reference counting mechanism:

Everything in Python is an object, and at its core is a structure: PyObject

typedef struct_object {
 int ob_refcnt;
 struct_typeobject *ob_type;
} PyObject;Copy the code

PyObject is mandatory for every object, where ob_RefCNt is used as a reference count. When an object has a new reference, its OB_refCNt increases, and when the object referencing it is deleted, its OB_refcnt decreases.

# define Py_INCREF (op) ((op) - > ob_refcnt + +) / / # define increase count Py_DECREF (op) \ / / reduce counting the if (- (op) - > ob_refcnt! = 0) \; \ else \ __Py_Dealloc((PyObject *)(op))Copy the code

When the reference count reaches 0, the object ends its life.

Advantages of reference counting mechanism:

  1. simple
  2. Once there is no reference in real-time, memory is freed directly. Don’t wait for a specific moment like other mechanics. Another benefit of real-time is that the time spent processing reclaimed memory is spread over the usual time.

Disadvantages of reference counting:

  1. Maintaining reference counts consumes resources
  2. A circular reference
list1 = []
list2 = []
list1.append(list2)
list2.append(list1)Copy the code