0

In my C++ code, I allocate a huge amount of memory to create a tree, and then I use 'delete' in every node to free the memory.

After I delete everything, I check the amount of used memory by the OS, and I see that the memory wasn't freed. That's expected, because the process doesn't return the memory to the OS right away, because it still might use it again.

The problem is that I create another huge tree right after I delete the first one, and instead of using the already allocated memory, the process allocates more memory from the OS. Because of this, I ran out of RAM and my computer got really slow, to the point where I couldn't even switch tabs. I checked the memory usage at several moments to see if that's what really happened, and it is.

If only I could make the process use the same memory it used before, or maybe if I could force the process to give the memory back to the OS.

What can I do to solve this problem?

(I'm using Debian)

I'm creating a tree of dynamic objects. The Node class has a vector to store the child nodes, among the other class variables:

std::vector child; The class destructor deletes all the dynamically allocated class variables, and then deletes the child nodes:

~Node() {
    //Deleting the other variables
                .
                .
                .

    //Deleting the child nodes
    for(int i = 0; i < child.size(); i++) {
        delete child[i];
    }
}

My class has a method that creates a tree of a given height, in which the current node is the root node:

void createTree(int height) {
    if(height == 0) {
        return;
    }
    for(int i = 0; i < numberOfChildNodes; i++) {
        child.push_back(new Node());
        child[i]->createTree(height - 1);
    }
}

This class has another method where I create a tree with height = 3, then I delete the entire tree and create another one with height = 4, then I delete the entire tree and create one with height = 5, and so on, until a memory limit is reached:

void highestTreePossible() {
    int i, height = 3;
    struct sysinfo memInfo;
    while(true) {
        createTree(height);
        sysinfo (&memInfo);
        if(memInfo.freeram > limit) {
            std::cout << "Highest tree possible: height = " << height;
            break;
        }
        for(i = 0; i < child.size(); i++) {
            delete child[i];
        }
        child.clear();
        height++;
    }
    for(i = 0; i < child.size(); i++) {
        delete child[i];
    }
    child.clear();
}
MrPontes
  • 29
  • 5
  • Please post a [mcve] of your program / problem. – Jesper Juhl May 19 '20 at 18:17
  • @JesperJuhl I posted the code in this other topic, back when I thought the problem was a memory leak. It's the same code. https://stackoverflow.com/questions/61892960/why-is-my-program-leaking-memory-working-with-trees-in-c – MrPontes May 19 '20 at 18:22
  • 2
    If `I couldn't even switch tabs` then the conclusion from the other question was wrong and you are actually leaking. Post a minimal repro example, or better, use `unique_ptr` all around to be sure you're not leaking. – Jeffrey May 19 '20 at 18:25
  • 4
    @MrPontes All Stackoverflow questions should be self contained. If that's the code that's relevant for *this* question it should be *in* this question. – Jesper Juhl May 19 '20 at 18:26
  • A minimal reproducible example means that someone can take the code and run it themselves and reproduce the problem. There may be a memory leak elsewhere in your program. – castro May 19 '20 at 19:04
  • Memory handling is OS specific. Which OS are you using? There are a lot of them out there. – Thomas Matthews May 19 '20 at 21:54
  • I suspect there is a memory leak in the code somewhere, and that is what is causing the OS to allocate more RAM after the first tree's nodes are (allegedly) deleted, rather than re-using the freed-up RAM again (which would be the expected behavior). That said, one way to force the OS to free up RAM would be to `mmap()` a shared-memory region, allocate all of your data inside that shared-memory region, and then `munmap()` it when you're done using it. – Jeremy Friesner Sep 02 '22 at 04:03

1 Answers1

3

How to force OS to claim back memory? (C++)

Terminate the process.

Other than that, there may be OS specific calls that can do it, but attempting to use those can interfere with the dynamic memory allocator implementation which may rely on being in control of the memory that it has.

... and instead of using the already allocated memory, the process allocates more memory from the OS.

If this is the case, then it seems like your program might have a memory leak, and that you actually don't delete everything. I recommend making sure that there are no leaks.


I posted the code in this other topic, back when I thought the problem was a memory leak. It's the same code.

The code is incomplete and it is not possible to conclusively tell that the complete program doesn't leak memory.

Back then you described the case differently, and based on that description there was no reason to assume that there is a memory leak.

eerorika
  • 232,697
  • 12
  • 197
  • 326