第一课、内存管理机制
一、课程介绍
1.1 课程概要
课程概要
课程目标
1.2 为什么要学习内存管理
内存管理的重要性
二、 Python的内存管理机制(重难点内容,多回听视频)
2.1 赋值语句内存分析
赋值语句内存分析
1 def extend_list(val, l=[]): 2 l.append(val) 3 return l 4 5 list1 = extend_list(10, []) 6 list2 = extend_list(123) 7 list3 = extend_list(‘a‘) 8 9 print(list1, id(list1)) 10 print(list2, id(list2)) 11 print(list3, id(list3))
2.2 垃圾回收机制
垃圾回收机制
1 class Cat(object): 2 def __init__(self): 3 print(‘对象产生了:{0}‘.format(id(self))) 4 def __del__(self): 5 print(‘对象删除了:{0}‘.format(id(self))) 6 7 def f0(): 8 """自动回收内存""" 9 while True: 10 c1 = Cat() 11 12 def f1(): 13 """一直在被引用,不会被回收""" 14 l = [] 15 while True: 16 c1 = Cat() 17 l.append(c1) 18 print(len(l)) 19 20 if __name__ == ‘__main__‘: 21 f0() 22 f1()
2.3 内存管理机制
原文:https://www.cnblogs.com/miaophp/p/12317982.html