I am implementing a Monte Carlo Tree Search algorithm in C . I create one huge tree at a time in a for loop, a different one at each iteration. My problem is that each tree is vast and if i create 12000 trees, my program crashes because all available memory in the PC is allocated.
The thing is, that the tree that i create in the iteration 5 for example, is useless in the next iterations, so i would like to free the memory it has allocated. I create each node as std::make_shared<Node<T, A, E>
where Node is a class that i have created, and the tree as an instance of a class mcts = MCTS(laneFreeState(state), backpropagation, terminationCheck,scoring)
CodePudding user response:
The call to std::make_shared
is using new
to allocate memory on the heap. So when you finish using the tree, just recursively iterate it, deleting the nodes as you go (deepest first, then work backwards).