XiaJun
XiaJun

Reputation: 1925

memory releasing in C++(VS2008)

Look at the following C++ code:

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Buf
{
public:
    Buf(size_t size)
    {
        _storage.reserve(size);
    }
    ~Buf()
    {
        vector<int> temp;
        _storage.swap( temp );//release memory
    }
    vector<int> _storage;
};

int main()
{
    int i = 0;
    while( ++i < 10000)
    {       
        Buf *buf = new Buf(100000);
        delete buf;
    }

    return 0;
}

I run it in debug mode(VS2008):when I set a break point in the line

//main function
int i = 0;

I find that the Process MyProgram.exe occupies about 300KB memory in Windows Task Manager.When I set a break point in the line

return 0;

The Process MyProgram.exe occupies about 700KB in Windows Task Manager.

My question is :why the memory that the program occupies increased?I think I have release the memory exactly~Why?

Upvotes: 2

Views: 79

Answers (2)

SomeWittyUsername
SomeWittyUsername

Reputation: 18368

The OS/Debug environment might employ optimization techniques and your memory releasing probably just returns it to pool; the actual memory release probably occurs on program termination

Upvotes: 3

user1773602
user1773602

Reputation:

Standard memory allocator will not (usually) release memory to OS when you deallocate it. Instead it will keep it for subsequent allocations for your process.

Thus you don't see memory ussage decrease in TM even though you deallocated it.

Upvotes: 3

Related Questions