1

I have a Python code which constructs a quadtree from the root node up, introducing new nodes as necessary.

This quadtree needs to be continually reconstructed What I have done thus far is, each time the quadtree needs to be reconstructed, I reset the list of children of the root node to an empty list, and build the tree starting from the root.

My worry is that, all the nodes of the previous tree (besides the root node) will still exist in memory. The tree is reconstructed perhaps tens of thousands of times over the course of the program and contains perhaps 5000 nodes on average, so I wouldn't be suprised if the memory gets overloaded.

In order to not exceed memory limitations, wouldn't I have to delete all the previous nodes somehow? How might I do this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
math_lover
  • 886
  • 7
  • 20
  • So you don't mean that you insert new nodes and somehow need to re-shape your tree, but that you are tearing down your tree (which for example been based on dataset A) and build a new one (based on B dataset)? – gsamaras Sep 08 '17 at 11:00
  • The way the program works is that the tree must be constructed from the root up each time. Once we reconstruct the tree using dataset B, we don't care about data from the nodes of the previous tree which used dataset A. What I have down is I don't "tear down" tree A. I don't rewrite over the nodes of tree A. I construct a completely new tree B starting from the same root node (I first reset its children to be empty). Hence, after constructing B, the node objects from both tree B and tree A are stored in memory, if I understand correctly. What I should do is delete tree A but I don't know how. – math_lover Sep 08 '17 at 11:05

1 Answers1

0

Use del, like this:

del children

which ensures that the list's memory will be released if the list children isn't referenced anywhere else.

You have to traverse through the whole tree though to delete every node, which could easily be done recursively.

Read more in how to release used memory immediately in python list?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • but how does this solve the problem? Each node of the tree has two associated lists: a list "data" which stores information about the node, and a list "children" which contains node objects. Even if I delete the list "children" of a node, the node's children (themselves node objects) will still be stored in memory. I would have to go through all of the node objects in the tree and delete them one by one, no? – math_lover Sep 08 '17 at 15:03