python 进阶(14) -- 内存管理机制

[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]那是,未满45秒的邂逅[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]转载[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]作者:Vamei 出处:http://www.cnblogs.com/vamei [HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]前言[HTML_REMOVED] [HTML_REMOVED]语言的内存管理是语言设计的一个重要方面。它是决定语言性能的重要因素。无论是C语言的手工管理,还是Java的垃圾回收,都成为语言最重要的特征。这里以Python语言为例子,说明一门动态类型的、面向对象的语言的内存管理方式[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]对象的内存使用[HTML_REMOVED] [HTML_REMOVED]赋值语句是语言最常见的功能了。但即使是最简单的赋值语句,也可以很有内涵。Python的赋值语句就很值得研究[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED]1[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]### 引用和对象[HTML_REMOVED] [HTML_REMOVED]为了探索对象在内存的存储,我们可以求助于Python的内置函数id()。它用于返回对象的身份(identity)。其实,这里所谓的身份,就是该对象的内存地址[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED]1[HTML_REMOVED]

[HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]id[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]a[HTML_REMOVED][HTML_REMOVED]))[HTML_REMOVED] [HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]hex[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]id[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]a[HTML_REMOVED][HTML_REMOVED])))[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]对象引用对象[HTML_REMOVED] [HTML_REMOVED]Python的一个容器对象(container),比如表、词典等,可以包含多个对象。实际上,容器对象中包含的并不是元素对象本身,是指向各个元素对象的引用[HTML_REMOVED] [HTML_REMOVED]我们也可以自定义一个对象,并引用其它对象:[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]class[HTML_REMOVED] [HTML_REMOVED]from_obj[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]object[HTML_REMOVED][HTML_REMOVED])[HTML_REMOVED][HTML_REMOVED]:[HTML_REMOVED] [HTML_REMOVED]def[HTML_REMOVED] [HTML_REMOVED]init[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]self[HTML_REMOVED][HTML_REMOVED],[HTML_REMOVED] [HTML_REMOVED]to_obj[HTML_REMOVED][HTML_REMOVED])[HTML_REMOVED][HTML_REMOVED]:[HTML_REMOVED] [HTML_REMOVED]self[HTML_REMOVED][HTML_REMOVED].[HTML_REMOVED][HTML_REMOVED]to_obj[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED]to_obj[HTML_REMOVED]

[HTML_REMOVED]b[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED][[HTML_REMOVED][HTML_REMOVED]1[HTML_REMOVED][HTML_REMOVED],[HTML_REMOVED][HTML_REMOVED]2[HTML_REMOVED][HTML_REMOVED],[HTML_REMOVED][HTML_REMOVED]3[HTML_REMOVED][HTML_REMOVED]][HTML_REMOVED] [HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED]from_obj[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]b[HTML_REMOVED][HTML_REMOVED])[HTML_REMOVED] [HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]id[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]a[HTML_REMOVED][HTML_REMOVED].[HTML_REMOVED][HTML_REMOVED]to_obj[HTML_REMOVED][HTML_REMOVED]))[HTML_REMOVED] [HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]id[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]b[HTML_REMOVED][HTML_REMOVED]))[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]引用减少[HTML_REMOVED] [HTML_REMOVED]某个对象的引用计数可能减少。比如,可以使用del关键字删除某个引用:[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]from[HTML_REMOVED] [HTML_REMOVED]sys[HTML_REMOVED] [HTML_REMOVED]import[HTML_REMOVED] [HTML_REMOVED]getrefcount[HTML_REMOVED]

[HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED][[HTML_REMOVED][HTML_REMOVED]1[HTML_REMOVED][HTML_REMOVED],[HTML_REMOVED] [HTML_REMOVED]2[HTML_REMOVED][HTML_REMOVED],[HTML_REMOVED] [HTML_REMOVED]3[HTML_REMOVED][HTML_REMOVED]][HTML_REMOVED] [HTML_REMOVED]b[HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED] [HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]getrefcount[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]b[HTML_REMOVED][HTML_REMOVED]))[HTML_REMOVED]

[HTML_REMOVED]del[HTML_REMOVED] [HTML_REMOVED]a[HTML_REMOVED] [HTML_REMOVED]print[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]getrefcount[HTML_REMOVED][HTML_REMOVED]([HTML_REMOVED][HTML_REMOVED]b[HTML_REMOVED][HTML_REMOVED]))[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]总结[HTML_REMOVED] [HTML_REMOVED]Python作为一种动态类型的语言,其对象和引用分离。这与曾经的面向过程语言有很大的区别。为了有效的释放内存,Python内置了垃圾回收的支持。Python采取了一种相对简单的垃圾回收机制,即引用计数,并因此需要解决孤立引用环的问题。Python与其它语言既有共通性,又有特别的地方。对该内存管理机制的理解,是提高Python性能的重要一步[HTML_REMOVED]