在 python 中将文本文件中的元素添加到字典时发生内存泄漏

memory leak while adding elements from text file to dictionary in python

我正在 运行 编写一个非常简单的代码来读取 txt 文件并将它们添加到现有词典中。使用 htop 我看到使用的内存线性增加,直到我 运行 内存不足。这是代码的简化版本:

import numpy as np
data = np.load(path_dictionary, allow_pickle=True)
dic = data.item()
for ids in dic:
    output = np.loadtxt(filename)
    array = output[:,1]
    dic[ids][new_info] = array

我尝试删除输出并在循环中添加垃圾收集器,但没有帮助。

    del output
    del array
    gc.collect()

我使用此 post 中的一个函数来获取字典在 100 次迭代前后的大小。 The original dictionary is 9GB and size increases about 13MB, htop that used memory recreases 10GB.该脚本应该读入大约 70K 个文件。

谁能帮我解决导致内存泄漏的问题以及可能的解决方案?

当您调用 array = output[:,1] 时,numpy 只会创建一个视图。这意味着它保留对整个(可能很大)output 的引用以及 array 只是第一列的信息。现在您将此引用保存到 dic 意味着仍然存在对整个输出的引用并且垃圾收集器无法释放内存。

要解决此问题,只需指示 numpy 创建一个副本即可:

array = output[:,1].copy()

这样数组将包含它自己的数据副本(这比创建视图慢),但关键是一旦你删除了 output(明确地通过 del output 或在下一次迭代中覆盖它),不再有对输出的引用,内存将被释放。

Python的垃圾回收器会自动清理不用的变量,但有些大容器(如list, dict)总是不能按预期回收。代码中的变量 array 导致内存泄漏,因为它创建了一个 每个循环对 output 的新引用,它被 dic 引用。所以你应该在深拷贝(而不是浅拷贝)后的每个循环中删除 array,并使 dic 引用副本。

from copy import deepcopy
for ids in dic:
  output = np.loadtxt(filename)
  array = output[:,1]
  array_c = deepcopy(array) 
  del array
  dic[ids][new_info] = array_c