Python文档在GC上:
并非所有在一些免费的列表中的项目可能会被释放,由于特定的实现,特别是int和float。
所以分配经由发电机一个极大无限整数会吃掉你所有的内存(最大接收内存错误),而不是所有的项目可能会被释放按照上面的定义。
但是,内存消失了吗?不可以。环境可以让代码重复使用。垃圾收集“高效”并不意味着它会收回物体离开作用域的时刻。它也可能意味着“让我们继续使用刚刚使用过的内存,也许这段愚蠢的代码会再次使用它。“
或者为Effbot告诉我们:这是返回给定的分配将由分配器被重用
内存,即使它没有返回到系统
您可以。 force a GC collection,但这实际上可能会阻碍表现,除非你知道为什么并且有一个非常非常好的理由来强制它。
gc.collect([generation])
With no arguments, run a full collection. The optional argument generation may be an integer specifying which generation to collect (from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned.
Changed in version 2.5: The optional generation argument was added.
Changed in version 2.6: The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular int and float.
Python对Integer的内存管理比“内存泄漏和垃圾收集”要复杂一点,你可能想看看这里的解释:http://www.laurentluce.com/posts/python-integer-objects -implementation/ –