跳转至

Python中的垃圾回收机制

之前写Python的时候都不怎么关注内存使用情况,多用点少用点都无所谓,反正程序执行完了就肯定都被回收了。 直到需要一个一直跑着的程序,才发现好像出现了内存泄漏的情况,内存会不停的增长,所以特地研究一下怎么能手动回收内存。 首先写一个获取系统内存的函数

def get_used_memory():
    """
    返回系统已用内存
    :return: 已用内存,MB
    """
    mem = psutil.virtual_memory()
    return mem.used / 1024 / 1024

一般处理图像比较多,搞一个OpenCV图像的数据

def get_test_data() -> np.ndarray:
    """
    获取一个测试数据
    :return: 
    """
    return np.random.randint(0, 255, size=(3000, 3000, 3))

写个程序测试一下内存,可以看到使用了2个G的内存

if __name__ == '__main__':
    before = get_used_memory()
    data = [get_test_data() for _ in range(10)]
    while True:
        after = get_used_memory()
        print(after - before)
        time.sleep(1)
2060.34765625
2061.18359375
2061.0546875
2061.4296875
2061.73828125
2061.65234375
2061.0390625
2061.09375
加一行看看data引用没了之后内存会不会释放
    before = get_used_memory()
    data = [get_test_data() for _ in range(10)]
    while True:
        after = get_used_memory()
        data = None
        print(after - before)
        time.sleep(1)
2063.84765625
1.38671875
1.515625
-0.48828125
0.359375
2.015625
2.16015625
2.3359375
2.58203125
2.8359375
可以看到还是释放了的。然后试试函数调用的情况
def test(data):
    for d in data:
        print(f"shape is {d.shape}")


if __name__ == '__main__':
    before = get_used_memory()
    data = [get_test_data() for _ in range(10)]
    test(data)
    while True:
        after = get_used_memory()
        data = None
        print(after - before)
        time.sleep(1)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
shape is (3000, 3000, 3)
2063.62109375
0.71875
普通的函数看起来没啥问题

评论