首页 > 编程语言 > 详细

Python 内存回收问题

时间:2020-03-10 00:08:00      阅读:98      评论:0      收藏:0      [点我收藏+]

今天遇见一个奇怪的问题,在python中,对自定义类型的局部变量竟然不进行垃圾回收

测试代码如下

# encoding=utf-8
from memory_profiler import profile
import gc
import time

class A(object):
    def __init__(self, id):
        self.id = id
        for i in range(100):
            setattr(self, "test_%s"%i, i)

@profile
def test():
    a = []
    for i in xrange(10000):
        _a = A(i)
        a.append(_a)

@profile
def test1():
    test()
    gc.collect()
    time.sleep(10)

if __name__ == ‘__main__‘:
    test1()

  

结果如下:

技术分享图片

 

 

在test1中调用test函数,理论上来说,调用完test后,test里的局部变量a应该被释放掉,但实际是没有被释放掉。以上在python2.7 和python3.6中都是一样的结论。

 

Python 内存回收问题

原文:https://www.cnblogs.com/xingkongyihao/p/12452476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!