垃圾回收机制_合集

#大整数对象池
b = 1500
a = 1254
print(id(a))
print(id(b))
b = a
print(id(b))

a1 = "Hello 垃圾机制"
a2 = "Hello 垃圾机制"
print(id(a1),id(a2))
del a1
del a2
a3 = "Hello 垃圾机制"
print(id(a3))

s = "Hello"
print(id (s))
s = "World"
print(id (s))
s = 123
print(id (s))
s = 12
print(id (s))

lst1 = [1,2,3]
lst2 = [4,5,6]
lst1.append(lst2)
lst2.append(lst1)#循环进行引用
print(lst1)
print(lst2)

class Node(object):
    def __init__(self,value):
        self.value = value
print(Node(1))
"""
创建一个新对象,python向操作系统请求内存,
python实现了内存分配系统,
在操作系统之上提供了一个抽象层
"""
print(Node(2))#再次请求,分配内存

import gc
class ClassA():
    def __init__(self):
        print('object born,id:%s'%str(hex(id(self))))
def f3():
    print("-----0------")
    # print(gc.collect())
    c1 = ClassA()
    c2 = ClassA()
    c1.t = c2
    c2.t = c1
    del c1
    del c2
    print("gc.garbage:",gc.garbage)
    print("gc.collect",gc.collect()) #显式执行垃圾回收
    print("gc.garbage:",gc.garbage)
if __name__ == '__main__':
    gc.set_debug(gc.DEBUG_LEAK) #设置gc模块的日志
    f3()

2020-05-08

原文地址:https://www.cnblogs.com/hany-postq473111315/p/12846950.html