知识点:首先需要知道,在python之一切东西皆对象.
1.小整数对象池:[-5,257]这些对象时提前建立好的,不会被垃圾回收.
eg a= 100 b =100 id(a)=id(b)
2.大整数对象池
定义大整数时,会创建新的对象
a = 2000 b =20000 id(a) ! = id(b)
3.inner机制(共用一份:字符和单个数字)
4.python 采用引用技术为主 标记-清楚和分代收集两种机制为辅
引用技术的核心 结构体
优点:简单 没有变量指向立即删除
缺点:维护引用技术消耗资源
循环引用无法解决
5.分代回收
三条链表 0 1 2
0 链表 用于保存刚创建的对象,然后将这些对象串起来, 每隔一段时间,列表减一 检测出引用不为且无相互引用 放到1 链表
1 链表 刷新多次 之后,存留下的放入链表2 中.
6.gc 模块中一些需要掌握的方法
eg : gc.getcount(1 ,2 ,3 ) 1清理预值 2 清理次数 达到某种场景就会清理
get_threshold()
Out[30]: (700, 10, 10) 3条链对象达到多少就会清理.
get__threshold(_,_,_) 设置执行垃圾回收频率。
7.sys getrefcount(a) 得到引用值. 会使2哦
8.引用加1的情况:
a = 23
b = a
func(a)ulist1 = [a,a]nc
9.引用减1的情况:
del a
a= 23
func(a)
1.小整数对象池
3.inner机制
a = 100
b = 100
print(id(a))
print(id(b))
c = "f"
d = "f"
print(id(c))
print(id(d))
e = "helloword"
f = "helloword"
print(id(e))
print(id(f))
g = "hello word"
h = "hello word"
print(id(g))
print(id(h))
typedef struct_object{
int ob_rencent;
struct_type object * ob_type;
}pyobject;
以下是两种循环引用会导致程序内存飙升.但要import gc gc.disable 关闭垃圾回收机制
class A:
def __init__(self):
pass
def f2():
while True:
c1 = A()
c2 = A()
c1.t = c2
c2.t = c1
del c1
del c2
f2()
list1 = []
list2 = []
list1.append(list2)
list2.append(list1)
gc
import gc
gc.get_count()
Out[5]: (274, 6, 3)
gc.get_count()
Out[6]: (315, 6, 3)
gc.get_count()
Out[7]: (346, 6, 3)
#下面就清理了一次
[26]: (628, 6, 3)
gc.get_count()
Out[27]: (643, 6, 3)
gc.get_count()
Out[28]: (35, 7, 3)
gc.get_count()
Out[29]: (47, 7, 3)