caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
800 stars 185 forks source link

Possible memory leak due to deepcopy ? #198

Open raquelhortab opened 2 years ago

raquelhortab commented 2 years ago

Hi, I am using a fair amount of trees (on some evolutionary computing task) and I find misself with some memory leak, I've traced the problem down to treelib's deepcopy. Is it possible that, when creating a Tree from another one (using the tree parameter and deep=True), it creates some link between the trees?

I perform loops on lists of trees, in each loop, I generate a new list and delete the previous one:

del list
gc.collect()

The new list is generated partially from trees from the previous list using Tree(tree=previous_tree, deep=True). The thing is that deleting the list does not free memory and my program's memory keeps increasing after each iteration. tracemalloc's stats suggest the problem is in treelib's deepcopy.

    child = TreeOperations.deep_copy(parent)
  File "/home/raquel/Documents/STUDIES/uned/uned/CE/A3/proj/trees.py", line 413
    tree = Tree(tree=a.tree, deep=True)
  File "/home/raquel/Documents/STUDIES/uned/uned/CE/venv/lib/python3.7/site-packages/treelib/tree.py", line 106
    new_node = deepcopy(node) if deep else node
  File "/usr/lib/python3.7/copy.py", line 180
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.7/copy.py", line 281
    state = deepcopy(state, memo)
  File "/usr/lib/python3.7/copy.py", line 150
    y = copier(x, memo)
  File "/usr/lib/python3.7/copy.py", line 241
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.7/copy.py", line 180
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.7/copy.py", line 307
    value = deepcopy(value, memo)
  File "/usr/lib/python3.7/copy.py", line 150
    y = copier(x, memo)
  File "/usr/lib/python3.7/copy.py", line 216
    append(deepcopy(a, memo))
  File "/usr/lib/python3.7/copy.py", line 169
    rv = reductor(4)
  File "/usr/lib/python3.7/uuid.py", line 208
    state = self.__dict__.copy()