简体   繁体   English

如何在C ++的递归函数中释放分配的内存

[英]How to free allocated memory in a recursive function in C++

I have a recursive method in a class that computes some stages (doesn't matter what this is). 我在一个类中有一个递归方法,可以计算某些阶段(这无关紧要)。 If it notice that the probability of success for a stage is to low, the stage will be delayed by storing it in a queue and the program will look for delayed stage later. 如果它注意到某个阶段成功的可能性很低,那么该阶段将通过将其存储在队列中而被延迟,并且程序将在以后寻找延迟的阶段。 It grabs a stage, copies the data for working and deletes the stage. 它抓住一个阶段,复制工作数据并删除该阶段。

The program runs fine, but I got a memory problem. 该程序运行正常,但出现内存问题。 Since this program is randomized it could happen that it delays up to 10 million stages which results in something like 8 to 12 GB memory usage (or more but it crashes before that happens). 由于该程序是随机的,因此可能会延迟多达1000万个阶段,从而导致大约8到12 GB的内存使用(或更多,但在此之前崩溃)。 It seems the program never frees the memory for a deleted stage before reaching the end of the call stack of the recursive function. 似乎该程序在到达递归函数的调用堆栈末尾之前从未为释放的阶段释放内存。

class StageWorker
{
private:
    queue<Stage*> delayedStages;

    void perform(Stage** start)
    {
       // [long value_from_stage = (**start).value; ]

       delete *start;

       // Do something here

       // Delay a stage if necessary like this
       this->delayedStages.push(new Stage(value));

       // Do something more here

       // Look for delayed stages like this
       Stage* front = this->delayedStages.front();
       this->delayedStages.pop();
       this->perform(&front);
    }
}

I use pointer to pointer as I thought the memory is not freed, because there is a pointer (front*) that points to the stage. 我以指针为指针,因为我认为不会释放内存,因为有一个指向舞台的指针(front *)。 So I used a pointer that point to the pointer, and so I can delete it. 因此,我使用了一个指向该指针的指针,因此可以删除它。 But it seems not to work. 但这似乎行不通。 If I watch the memory usage on Performance monitor (on Windows) it like like this: 如果在性能监视器(在Windows上)上查看内存使用情况,则如下所示: 递归调用明显结束的内存使用情况
I marked the end of the recursive calls. 我标记了递归调用的结尾。 This is also just a example plot. 这也是一个示例图。 real data, but in a very small scenario. 真实数据,但情况非常小。

Any ideas how to free the memory of not longer used sages before reaching the end of the call stack? 有什么想法如何在到达调用堆栈末尾之前释放不再使用的贤者的内存?

Edit 编辑
I followed some advice and removed the pointers. 我遵循了一些建议,并删除了指针。 So now it looks like this: 所以现在看起来像这样:

class StageWorker
{
private:
    queue<Stage> delayedStages;

    void perform(Stage& start)
    {
       // [long value_from_stage = start.value; ]

       // Do something here

       // Delay a stage if necessary like this
       this->delayedStages.push(Stage(value));

       // Do something more here

       // Look for delayed stages like this          
       this->perform(this->delayedStages.front());
       this->delayedStages.pop();
    }
}

But this changed nothing, memory usage is the same as before. 但这并没有改变,内存使用情况与以前相同。

As mentioned it maybe is a problem of monitoring. 如前所述,这可能是监视问题。 Is there a better way to check the exact memory usage? 有没有更好的方法来检查确切的内存使用情况?

Just to close this question as answered: 只是为了结束回答这个问题:

Mats Petersson (thanks a lot) mentiond in the comments that it could be a problem of monitoring the memory usage. Mats Petersson(非常感谢)在评论中提到,这可能是监视内存使用情况的问题。

In fact this exactly was the problem. 实际上,这正是问题所在。 The code I provided in the edit (after replacing the pointers by references) frees the space correctly, but it is not given back to the OS (in this case Windows). 我在编辑中提供的代码(在用引用替换了指针之后)可以正确释放空间,但不会将其返回给操作系统(在本例中为Windows)。 So from the monitor it looks like the space is not freed. 因此,从监视器看似乎没有释放空间。

But then I made some experiments and saw that freed memory is reused by the application, so it does not have to ask the OS for more memory. 但是随后我做了一些实验,发现释放的内存被应用程序重用,因此不必要求操作系统提供更多的内存。

So what the monitor shows is the maximum memory used bevore getting back the whole memory the recursive function required, after the first call of this function ends. 因此,该监视器显示的是在该函数的第一次调用结束后,在获得整个内存所需的递归函数之前,已使用的最大内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM