Cloud computing Python Automation: Internal Reference counting

Source: Internet
Author: User

Python internally records how many references are in use for all objects. An internal tracking variable, called a reference counter. When the object is created, a reference count is created, and when the object is no longer needed, that is, the reference count of the object becomes 0 o'clock, and it is garbage collected. (This is just the image of the said, not strictly 100% correct, but the popular understanding is often the best way to learn)

To increase the reference count:

When an object is created and assigned to a variable (referencing it), the object's reference technique is set to 1. When an application of the same object is either assigned to another variable, or passed as a parameter to a function, method, or class instance, or is assigned a member of a Window object, a new reference to that object, or alias, is created (the object's reference count is automatically added 1)

To reduce the reference count:

When a reference to an object is destroyed, the reference count is reduced. The most obvious example is that when a reference leaves its scope, this is most often the case at the end of the function, when all local variables are automatically destroyed, and the reference count of the object decreases.

When a variable is assigned to another object, the source object's reference technology is automatically reduced by 1

Other ways to reduce the reference count of objects include using the DEL statement to delete a variable, or when the reference count of an object is reduced in the following cases:

    1. A local reference leaves its scope, such as the end of a function

    2. The object's alias is displayed for destruction

    3. An alias of an object is assigned to another object

    4. object is removed from a Window object

    5. The Window object itself is destroyed


>> Import Sys

>> a= "AB"

>> sys.getrefcount ("AB")

3 for the first time the result is 3

>> b= "AB"

>> sys.getrefcount ("AB")

4 second result +1

>> b=0 B refers to other objects (0), and for "AB" it cancels a reference

>> sys.getrefcount ("AB")

3 results based on the last reference-1

Note: Object references with spaces in the interactive interpreter are always 3, but return to normal in the script, for example: #!/usr/bin/env python # coding=utf8 FDAF import sys print sys.getrefcount ("AB CD ") a=" AB CD "Print Sys.getrefcount (" AB CD ") b=" AB CD "Print Sys.getrefcount (" AB CD ") c=b print Sys.getrefcount (" AB CD ")

Garbage collection:

Memory that is no longer being used is freed by a mechanism called garbage collection. As stated above, although the interpreter tracks the reference count of the object, the garbage collector is responsible for freeing the memory. The garbage collector is a separate piece of code that is used to look for objects with a reference count of 0, and he is also responsible for checking those objects that are destroyed if the reference count is greater than 0. A specific situation can cause a circular reference.

A circular reference occurs when you have at least two objects referencing each other, that is, when the references are all gone, the references still exist, which means that the reference count alone is not enough. The Python garbage collector is actually a reference counter and a cyclic garbage collector. When the reference count of an object becomes 0, the interpreter pauses, releases the object, and only other objects accessible by the object, as a supplement to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by reference counting). In this case, the interpreter pauses to try to clean up all the loops that are referenced.

Cloud computing Python Automation: Internal Reference counting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.