clearing memory allocated in stack in c++
I was implementing a chess bot in c++ using recursive algorithms and the program evaluates over a million nodes per move.
Over time the memory it takes up gets to over 1 GIG of RAM...
But I don't really need the variables that were previously declared after I'm done with the move...开发者_如何学JAVA
So how do I manually flush the stack memory to get rid of the previously declared variables on the stack just like java's garbage collector?
UPDATE
I found out that there's this line in my source:
Move * M = new Move(x1,y1,x2,y2);
pair <Move *, Piece *> pr (M,aPiece);
and it's in the perform move function which gets called a million times in the recursion...
My question is, how would you clear such variable once all the recursion is done and I no longer need this variable, but while the recursion is doing its thing, I need that variable to stay in the memory?
Stack-based storage is reclaimed as soon as the function call in which it resides returns.
Is it possible that you were using heap-allocated memory (i.e. calling new
) within your recursive function? Alternatively, if you're simply looking at the Windows Task Manager or an equivalent, you may be seeing "peak" usage, or seeing some delay between memory being freed by your program and returned to the OS memory pool.
Follow up (after question was edited):
It's not clear what you are doing with the pair<Move*, Piece*>
, so I can't tell if the Move objects need to be held by pointer or not. The primary reasons for holding them via pointer would be polymorphism (not used here since you don't appear to be creating subclass objects) and to allow their lifetime to be independent of the call stack. Sounds like you don't have that reason, either. So, why not:
std::pair<Move, Piece*> pr(Move(x1,y1,x2,y2), aPiece);
I'm assuming you have some temporary variables which you don't need to still have around after the recursive call returns. If this is the case, you can declare them in their own scope within the function. Once you leave that scope, the automatic variables declared in it will not take up space in the stack anymore.
int recursive_function(int a, int b){
int total;
{ //new scope
int temp[20];
//do stuff using temp and total
} //temp goes out of scope, not taking up space on stack anymore
total += recursive_function(a+total,b);
{
int temp[25];
//do stuff with this other temp
}
total += recursive_function(a,b+total);
return total;
}
EDIT:
In response to your update, if you allocate memory with new
, you need to free it with delete
when you are done with it. In this case the line would be delete M;
void recursive_function(pair <Move *, Piece *> last_move){
Move * M = new Move(x1,y1,x2,y2);
pair <Move *, Piece *> pr (M,aPiece);
recursive_function(pr);
delete M;
}
You don't - there is no way of disposing of the stack the compiler implements. You need to implement your own stack, which you can of course do with as you will.
I think it's far more likely that your "stack"-allocated objects have nothing to do with this, and that in fact you are forgetting to delete
something that you previously new
d. Perhaps that Move
object?
Concerning the update: why Move*
and new Move
? Move
sounds like a value object, which can be copied. Ditto Piece
(but this is less certain---you could have unique instances for
each Piece
).
精彩评论