Python garbage Collection

Source: Internet
Author: User

# garbage Collection

# Small Integer Object pool

# a = 100
# python defines small integers as [-5,257], these certificate objects are created in advance, are not garbage collected, and in a Python program, all that is formally used within this range is an object

# Large Integer Object pool
# Each large integer creates a new object
# >>> A = 1000
# >>> ID (A)
# 1797282549648
# >>> B = 1000
# >>> ID (B)
# 1797289269392

# Intern Mechanism
# >>> a = ' Hello '
# >>> B = ' Hello '
# >>> ID (a)
# 1797289499256
# >>> ID (b)
# 1797289499256


# >>> B = ' Hello World '
# >>> a = ' Hello World '
# >>> ID (a)
# 1797289512176
# >>> ID (b)
# 1797289512112


Summary
# small integer [-5,257) Common object, Resident memory
# single character shared object, resident memory
# single word, non-modifiable, default on intern mechanism, common object, reference count of 0, destroy

# GC garbage collection (garbage collection)

# Reference counting mechanism:
# Every thing in Python is an object, and their core is a struct: pyobject
# typedef struct_object{
# int OB_REFCNT
# Struct_typeobject *ob_type
#} Pyobject
# Pyobject is a required content for each object, where ob_refcnt is counted as a reference. When an object has a new reference, its ob_refcnt
# will increase, and when the object referencing it is deleted, his ob_refcnt will be reduced
# when the reference count is 0 o'clock, the object's life is over


# The reference counting mechanism is a bit:
# simple
# Real-time sex. Once there is no reference, the memory is released directly. There is no need to wait for a specific time for other mechanisms. Real-time also brings a benefit: the time it takes to process the recovered memory is allocated to the usual.


# import GC
# class ClassA ():
# def __init__ ():
# print ("Object born,id:%s"%str (Hex (ID (self)))

# def F2 ():
# while True:
# C1 = ClassA ()
# C2 = ClassA ()
# c1.t = C2
# c2.t = C1
# del C1
# del C2
# gc.disable ()
# F2 ()


# saying Ruby and Python garbage collection

# Generational Recycling
# Python is based on the reference counting mechanism, which collects two mechanisms supplemented by the second generation.


# 0-----------------------------------------------performed 100 times-1 cycles, reference count cannot be 0, see if circular reference? If it is a circular reference, 1

# 1-----------------------------------------------executed 1 times

# 2--------------------------------------------


Import GC
Print (Gc.get_count ()) #获取当前自动执行垃圾回收的技术器, returns a list with a length of 3
# (243, 3, 1)
Print (Gc.get_count ())


Print (Gc.get_threshold ()) # Gets the frequency of automatically performing garbage collection in the GC module
# (700, 10, 10)


Print (Gc.set_threshold ()) #设置自动执行垃圾回收的频率


# 1. The case that causes reference count +1
# 1. Objects are created, such as a=23
# 2. Objects are referenced, such as B=a
# 3. The object is passed as a parameter into a function, such as func (a)
# 4. Object as an element, stored in a container, list=[a,a]

# 2. Result in reference count-1
# object alias Bai display destroy column like Del a
The alias of the # object is assigned a new object, such as a = 23,
# An object leaves his scope, such as when the F function finishes executing, the local variable in the Func function (global variable does not)
# The container where the object resides is destroyed, or the object is deleted from the container

# Automatic garbage collection mechanism for GC modules
# You have to import the GC
# gc.enable () Open
# gc.disabled () disabled
# gc.isenabled () whether to turn on GC
# gc.collect () collect garbage (manually clean up garbage)


# gc.garbage () show garbage cleanup


# import Sys
# a = "Hello World"
# b = C
# Sys.getrefcount (a) The resulting value-1 is the true number of references

Python garbage Collection

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: info-contact@alibabacloud.com 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.