0
votes

I have a tree class in python3, each tree object has a reference to its parent and a list of references to its children.

In the main method there is a reference to the root of the tree. I tried to dereference the children of the root by saying root.children = [], but the RAM usage does not seem to go down. How can I clear the whole tree or a partial portion from memory? For those wondering this is important because the tree is very big and the RAM usage becomes too high.

p.s.: I believe that what is making the memory not being cleared is that after I remove the children from the root, depth 2 nodes have a reference to depth 3 nodes and vice-versa, and so on.

1
You can technically force GC as rohit mentions below, but I strongly doubt this is the "correct" solution. Almost certainly, there's a better solution that doesn't abuse GC, but there's no way to tell what that might be without a minimal reproducible example illustrating the problem and showing what you're trying to accomplish. - ggorlen

1 Answers

0
votes

You can try first using del on the children node and forcing the Garbage Collector to release unreferenced memory with gc.collect().