垃圾回收机制
内存管理机制
Python垃圾回收机制
>>> a = 1 >>> id(a) 4510816880 >>> hex(id(a)) '0x10cdd9a70'>>> a = 1 >>> b = 1 >>> a is b True >>> a = "hello" >>> b = "hello" >>> a is b True >>> a = "long long long long string" >>> b = "long long long long string" >>> a is b False >>> a = [] >>> b = [] >>> a is b False>>> from sys import getrefcount >>> a = [1, 2, 3] >>> print(getrefcount(a)) 2 >>> b = a >>> print(getrefcount(a)) 3 >>> print(getrefcount(a)) 3 >>>>>> from sys import getrefcount >>> a = [1, 2, 3] >>> getrefcount(a) 2 >>> b = [1, 2, a] >>> getrefcount(a) 3 >>> id(b[2]) 4518754888 >>> id(a) 4518754888
# 通过gc模块的get_threshold()方法,查看该阈值: >>> import gc >>> gc.get_threshold() (700, 10, 10)import gc gc.set_threshold(700, 10, 5)
参考
最后更新于